I was able to use comparator and have the data in Map> and work out a solution
public class SortMapOnValueInteger {
public static void main(String[] args) {
Map<String, List<Integer>> unsortMap = new HashMap<String, List<Integer>>();
unsortMap.put("z", new ArrayList(Arrays.asList( 3, 1, 56)));
unsortMap.put("b", new ArrayList(Arrays.asList( 0, 2, 65)));
unsortMap.put("x", new ArrayList(Arrays.asList( 0, 2, 35)));
unsortMap.put("a", new ArrayList(Arrays.asList( 3, 3, 12)));
unsortMap.put("c", new ArrayList(Arrays.asList( 1, 4, 65)));
unsortMap.put("d", new ArrayList(Arrays.asList( 2, 4, 65)));
unsortMap.put("e", new ArrayList(Arrays.asList( 4, 3, 34)));
unsortMap.put("y", new ArrayList(Arrays.asList( 4, 2, 23)));
unsortMap.put("n", new ArrayList(Arrays.asList( 3, 1, 23)));
unsortMap.put("j", new ArrayList(Arrays.asList( 2, 2, 54)));
unsortMap.put("m", new ArrayList(Arrays.asList( 1, 3, 96)));
unsortMap.put("f", new ArrayList(Arrays.asList( 0, 4, 54)));
System.out.println("Unsort Map......");
printMap(unsortMap);
System.out.println("\nSorted Map......");
Map<String, List<Integer>> sortedMap = sortByComparator(unsortMap);
printMap(sortedMap);
}
private static Map<String, List<Integer>> sortByComparator(Map<String, List<Integer>> unsortMap) {
// Convert Map to List
List<Map.Entry<String, List<Integer>>> list;
list = new ArrayList<>(unsortMap.entrySet());
// Sort list with comparator, to compare the Map values
Collections.sort(list, new Comparator<Map.Entry<String, List<Integer>>>() {
public int compare(Map.Entry<String, List<Integer>> o1,
Map.Entry<String, List<Integer>> o2) {
int comparison = Integer.compare(o1.getValue().get(0), o2.getValue().get(0));
if (comparison != 0) return comparison;
comparison = Integer.compare(o1.getValue().get(1), o2.getValue().get(1));
if (comparison != 0) return comparison;
return Integer.compare(o1.getValue().get(2), o2.getValue().get(2));
}
});
// Convert sorted map back to a Map
Map<String, List<Integer>> sortedMap = new LinkedHashMap<String, List<Integer>>();
for (Iterator<Map.Entry<String, List<Integer>>> it = list.iterator(); it.hasNext();) {
Map.Entry<String, List<Integer>> entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
public static void printMap(Map<String, List<Integer>> map) {
for (Map.Entry<String, List<Integer>> entry : map.entrySet()) {
System.out.println("[Key] : " + entry.getKey()
+ " [Value] : " + entry.getValue());
}
}
}
the output of sorted map is
Unsort Map......
[Key] : f [Value] : [0, 4, 54]
[Key] : d [Value] : [2, 4, 65]
[Key] : e [Value] : [4, 3, 34]
[Key] : b [Value] : [0, 2, 65]
[Key] : c [Value] : [1, 4, 65]
[Key] : a [Value] : [3, 3, 12]
[Key] : n [Value] : [3, 1, 23]
[Key] : m [Value] : [1, 3, 96]
[Key] : j [Value] : [2, 2, 54]
[Key] : z [Value] : [3, 1, 56]
[Key] : y [Value] : [4, 2, 23]
[Key] : x [Value] : [0, 2, 35]
Sorted Map......
[Key] : x [Value] : [0, 2, 35]
[Key] : b [Value] : [0, 2, 65]
[Key] : f [Value] : [0, 4, 54]
[Key] : m [Value] : [1, 3, 96]
[Key] : c [Value] : [1, 4, 65]
[Key] : j [Value] : [2, 2, 54]
[Key] : d [Value] : [2, 4, 65]
[Key] : n [Value] : [3, 1, 23]
[Key] : z [Value] : [3, 1, 56]
[Key] : a [Value] : [3, 3, 12]
[Key] : y [Value] : [4, 2, 23]
[Key] : e [Value] : [4, 3, 34]