Bonjour,

Je viens de parcourir plusieurs forum et j'ai mis en place deux solutions qui ne fonctionne pas. Je souhaiterais en effet, pouvoir trier une map de type <Long, String> par rapport aux données (String). Par ordre alphabétique.


J'ai utilisé :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
public static Map<Long, String> sortMap(Map<Long, String> aMap) {
Map myMap = new HashMap<Long, String>();
		TreeSet set = new TreeSet(new Comparator() {
			public int compare(Object obj, Object obj1) {
				String val1 = (String) ((Map.Entry) obj).getValue();
				String val2 = (String) ((Map.Entry) obj1).getValue();
				return val1.compareTo(val2);
			}
		});
 
		set.addAll(aMap.entrySet());
 
		for (Map.Entry e : aMap.entrySet()) {
			myMap.put(e.getKey(), e.getValue());
		}
 
		System.out.println("Before tree " + aMap);
 
		System.out.println("After tree " + aMap);
 
		return myMap;
}
Mais cela me renvoie exactement la même map non triée...