Created
          February 2, 2012 02:23 
        
      - 
      
 - 
        
Save osamingo/1720981 to your computer and use it in GitHub Desktop.  
    MapのValueでSortしたい
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | 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