对map键值进行排序

/** * 值排序 /** public static Map<Object,String> sortMap(Map oldMap) { ArrayList<Map.Entry<Object, String>> list = new ArrayList<Map.Entry<Object, String>>(oldMap.entrySet()); Collections.sort(list, new Comparator<Map.Entry<Object, String>>() { @Override public int compare(Entry<Object, String> arg0, Entry<Object, String> arg1) { long arg0long = 0; long arg1long=0; try { arg0long = ProjectDate.currtureDate(arg0.getValue().toString()).getTime(); arg1long=ProjectDate.currtureDate(arg1.getValue().toString()).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return (int)arg0long - (int)arg1long; } }); Map<Object,String> newMap = new LinkedHashMap<Object,String>(); for (int i = 0; i < list.size(); i++) { newMap.put(list.get(i).getKey(), list.get(i).getValue()); } return newMap; } /** * 键排序 */
static void mapSort( Map<String, Object> yourMap){
    Map<String, Object> map = new LinkedHashMap<String, Object>(); 
    //键的集合
    List<String> keyList = new ArrayList<String>(yourMap.keySet()); 
    //值的集合
    List<Object> valueList = new ArrayList<Object>(yourMap.values()); 
    Set<String> sortedSet = new TreeSet<String>(keyList); 

   Object[] sortedArray = sortedSet.toArray(); 
        int size = sortedArray.length; 

 for (int i = 0; i < size; i++) { 
       map.put(keyList.get(keyList.indexOf(sortedArray[i])), valueList.get(keyList.indexOf(sortedArray[i]))); 
   } 

       Set ref = map.keySet(); 
       Iterator it = ref.iterator(); 

       while (it.hasNext()) { 
       String i = (String) it.next(); 
       System.out.println(i+":"+map.get(i)); 
    } 
 } 

你可能感兴趣的:(mapsort)