Re: [xwiki-users] How to sort hashmap with velocity?
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 <[email protected]> a écrit : Objet: Re: [xwiki-users] How to sort hashmap with velocity? À: "Pascal BASTIEN" <[email protected]>, "XWiki Users" <[email protected]> Date: Jeudi 19 janvier 2017, 14h58 On Thu, Jan 19, 2017 at 11:25 AM, Pascal BASTIEN <[email protected]> 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:%2...
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
Note that if you are using XWiki 8.3+ you don't need to go trough xcontext to pass data between velocity macro and other script macros. Velocity variables ends up in Groovy (as global variables) and the opposite. On Tue, Jan 24, 2017 at 5:12 PM, Pascal BASTIEN <[email protected]> wrote:
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 <[email protected]> a écrit :
Objet: Re: [xwiki-users] How to sort hashmap with velocity? À: "Pascal BASTIEN" <[email protected]>, "XWiki Users" <[email protected]> Date: Jeudi 19 janvier 2017, 14h58
On Thu, Jan 19, 2017 at 11:25 AM, Pascal BASTIEN <[email protected]> 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:%2...
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
-- Thomas Mortagne
participants (2)
-
Pascal BASTIEN -
Thomas Mortagne