Sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static List<Map.Entry<String, Integer>> sortByValue(Map<String, Integer> unsortMap) | |
{ | |
List<Map.Entry<String, Integer>> list = new LinkedList<>(unsortMap.entrySet()); | |
// Sorting the list based on values | |
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() | |
{ | |
@Override | |
public int compare(Map.Entry<String, Integer> o1, | |
Map.Entry<String, Integer> o2) | |
{ | |
return o1.getValue().compareTo(o2.getValue()); | |
} | |
}); | |
return list; | |
} | |
for (Entry<String, Integer> e : sortByValue(map)) | |
{ | |
output += String.format("%s: %d\n", e.getKey(), e.getValue()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment