Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort the values of a HashMap freemarker template

I have in java this HashMap:

HashMap<String, String> map = new HashMap<String, String>();

     map.put("k1",  "3");
     map.put("k2", "4");
     map.put("k3", "2");
     map.put("k4", "6");
     map.put("k5", "1");
     map.put("k6", "5");

I print with freemarker template in this mode:

<#list map?values as v>
${v} - 
</#list>

but it prints in this order:

2 - 6 - 1 - 5 - 3 - 4

I would like to print in this order:

1 - 2 - 3 - 4 - 5  -6

how can I sort values ​​with with freemarker template?

like image 339
ersecchio Avatar asked May 08 '11 13:05

ersecchio


1 Answers

Try this:

<#list map?values?sort as v>
    ${v} - 
</#list>

Notice the use of the sort builtin for the sequence of values.

like image 120
Laurent Pireyn Avatar answered Oct 12 '22 04:10

Laurent Pireyn