Skip to content

Instantly share code, notes, and snippets.

@osamingo
Created February 2, 2012 02:23
Show Gist options
  • Save osamingo/1720981 to your computer and use it in GitHub Desktop.
Save osamingo/1720981 to your computer and use it in GitHub Desktop.
MapのValueでSortしたい
Map<String, Integer> scores = new HashMap<String, Integer>();
List<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>(scores .entrySet());
// Valueの降順(大きい順)に並べる
Collections.sort(entries, new Comparator<Object>() {
@Override
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
Map.Entry<String, Integer> e1 = (Map.Entry<String, Integer>) o1;
Map.Entry<String, Integer> e2 = (Map.Entry<String, Integer>) o2;
return ((Integer) e2.getValue()).compareTo((Integer) e1.getValue());
}
});
for (Map.Entry entry : entries) {
// entry.getKey() と entry.getValue() で取出し
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment