7

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
ersecchio
  • 71
  • 1
  • 2

5 Answers5

6

Try this:

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

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

Laurent Pireyn
  • 6,735
  • 1
  • 29
  • 39
4

If you are displaying the values independent of the keys then you can take the values out of the map and construct a TreeSet from it. Then the values would be in order.

TreeSet ordered =  new TreeSet(map.values());

Then

 <#list ordered as v>
     ${v} - 
 </#list>
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
2

This behaviour is predictable as HashMaps are not ordered. As has been pointed out in the comments a SortedMap e.g. TreeMap will sort on the keys. So you're going to have to cut some code to sort this.

See Sort a Map<Key, Value> by values (Java)

Community
  • 1
  • 1
planetjones
  • 12,469
  • 5
  • 50
  • 51
0

HashMap is not ordered. You should use a TreeMap instead which implements a SortedMap:-

SortedMap<String, String> map = new TreeMap<String, String>();

Please note - Sorted Map provides a total ordering on its keys. You cannot have it sorted on the values. If you want to sort on values you can always do that by sorting on the Map.entrySet() using a Comparable .

Also take a look at this very similar question: TreeMap sort by value

Community
  • 1
  • 1
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
  • `TreeMap` isn't "backed by" `SortedMap`, it implements it `` – skaffman May 08 '11 at 14:14
  • -1: `SortedMap`s are sorted on keys, not values. This is not what is asked. – Laurent Pireyn May 09 '11 at 08:45
  • @Laurent Pireyn - Care to read the full answer. I have clearly mentioned that it's sorted on keys and gave the example of how to sort on values! Please read answers fully before down voting. – CoolBeans May 09 '11 at 13:53
  • @CoolBeans: I read the full answer, but still think it doesn't answer the question. You talk about sorted maps, but then admit it's not what the OP wants, and redirect him to a related question. A valid answer should provide a complete solution to the question, or at least a summary of such a solution with links to the complete version. – Laurent Pireyn May 09 '11 at 14:32
  • @Laurent Pireyn - It's mentioned in the answer with how to solve it with Map.entrySet() and Comparable. That's why I said read the full answer! – CoolBeans May 09 '11 at 15:43
0

If you need iteration over a HashMap in predictable order, you can use a LinkedHashMap with predictable iteration order, it maintains a doubly-linked list running through all of its entries.

rsp
  • 23,135
  • 6
  • 55
  • 69
  • @Laurent, there are multiple ways to achieve a result, a hashmap that reproduces insertion order can be very useful when you need to have control over template expansion. Sort order only goes so far for non-trivial situations. – rsp May 09 '11 at 08:53
  • A map that conserves insertion order can be useful indeed, but that is still not what is asked here. The OP asks how to *sort* the values of the map, not how to conserve the order of their insertion. – Laurent Pireyn May 09 '11 at 09:05