Ok, then I inesert groovy code between 2 velocity bloc to sort my map: ... #end ##Fin foreach $xcontext.put("xSiglesArray", $SiglesArray) #end ## fin temporaire pour insérer le code groovy {{/velocity}}
{{groovy}} if(xcontext.get("xSiglesArray")){ SiglesArrayGv = xcontext.get("xSiglesArray"); // Trie le tableau (insensitive case ) SiglesArrayGv=SiglesArrayGv.sort { it.key.toLowerCase() }; xcontext.put("xSiglesArray", SiglesArrayGv); xcontext.put("DoRegen", true); } {{/groovy}} {{velocity}} ## ##Regénére la page et trie les entrées (suite) #if ( $xcontext.get("DoRegen")== 'true' && $xcontext.get("xSiglesArray") ) ## #set ($SiglesArray= $xcontext.get("xSiglesArray")) #foreach($Anchor in $AnchorsArray) ## Thxs -------------------------------------------- En date de : Jeu 19.1.17, Thomas Mortagne <thomas.morta...@xwiki.com> a écrit : Objet: Re: [xwiki-users] How to sort hashmap with velocity? À: "Pascal BASTIEN" <pbasnews-xw...@yahoo.fr>, "XWiki Users" <users@xwiki.org> Date: Jeudi 19 janvier 2017, 14h58 On Thu, Jan 19, 2017 at 11:25 AM, Pascal BASTIEN <pbasnews-xw...@yahoo.fr> wrote: > Hello, > > with velocity I have a hashmap: sort of variable with (key:value) that I want to sort, then I use: > #set ($myHashMap = $collectionstool.getSortedMap(String, String)) > $myHashMap.put('Mykey3','My value 3') > http://platform.xwiki.org/xwiki/bin/view/ScriptingDocumentation/?url=http:%2F%2Fnexus.xwiki.org%2Fnexus%2Fservice%2Flocal%2Frepositories%2Fpublic%2Farchive%2Forg%2Fxwiki%2Fcommons%2Fxwiki-commons-velocity%2F8.2.1%2Fxwiki-commons-velocity-8.2.1-javadoc.jar%2F!%2Forg%2Fxwiki%2Fvelocity%2Ftools%2FCollectionsTool.html%23getSortedMap-- > > It's working well ... except with uppercase because I want a sort with insensitive case. > > I tried to decrypt Javadoc and I found I could use: String.CASE_INSENSITIVE_ORDER > #set ($myHashMap = $collectionstool.getSortedMap(String.CASE_INSENSITIVE_ORDER, String)) > #set ($myHashMap = $collectionstool.getSortedMap(String::compareToIgnoreCase, String)) with java8 > but unfortunaly it doesn't work (I have xwiki error) Velocity is very far from supporting Java syntax. Plus here you seems to be mixing CollectionsTool and TreeMap javadoc. CollectionsTool contains only one getSortedMap() method and it has no parameter. There does not seems to be any way to get a TreeMap configured with String.CASE_INSENSITIVE comparator in Velocity. The main issue is that you can't create an object or access a static variable in Velocity so no way to use a method that takes a Comparator as parameter even if there was one (which is not the case for maps). Right now the only alternatives seems to be: * use Groovy * write a script service in Java that provide the feature * patch CollectionsTool to add a way to pass a comparator to the various methods > > Any idea to sort my hashmap with with insensitive case? > > (Unfortunaly the better way I found is an ugly way: I mix velocity with a piece of groovy but I'm not satisfied at all because I use velocity loop and other if.) > > Thxs for any help. > > Pascal B -- Thomas Mortagne