01: package org.julp.util.common;
02:
03: import java.util.Comparator;
04: import java.util.Set;
05: import java.util.Map;
06: import java.util.Map.Entry;
07: import java.util.Arrays;
08: import java.util.Collections;
09: import java.util.LinkedHashMap;
10: import java.io.Serializable;
11:
12: public class MapValueSorter implements Serializable {
13:
14: MapValueComparator comp = new MapValueComparator();
15:
16: public MapValueSorter() {
17: }
18:
19: public Map sort(Map map) {
20: Set set = map.entrySet();
21: int len = set.size();
22: Map.Entry[] entries = (Map.Entry[]) set
23: .toArray(new Map.Entry[len]);
24: Arrays.sort(entries, comp);
25: LinkedHashMap lhm = new LinkedHashMap(len);
26: for (int i = 0; i < len; i++) {
27: lhm.put(entries[i].getKey(), entries[i].getValue());
28: }
29: set = null;
30: map = null;
31: entries = null;
32:
33: return lhm;
34: }
35: }
|