Here you can find the source of sortedScoreMap(HashMap
Parameter | Description |
---|---|
unSortedMap | a parameter |
public static LinkedHashMap<Long, Double> sortedScoreMap(HashMap<Long, Double> unSortedMap)
//package com.java2s; //License from project: Open Source License import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map.Entry; public class Main { /** this function takes unsorted map as input and returns a sorted map * @param unSortedMap//from w ww . j ava2 s . c o m * @return */ public static LinkedHashMap<Long, Double> sortedScoreMap(HashMap<Long, Double> unSortedMap) { LinkedList<Entry<Long, Double>> mapList = new LinkedList<Entry<Long, Double>>(unSortedMap.entrySet()); Collections.sort(mapList, new Comparator<Entry<Long, Double>>() { @Override public int compare(Entry<Long, Double> o1, Entry<Long, Double> o2) { // TODO Auto-generated method stub return o1.getValue().compareTo(o2.getValue()); } }); LinkedHashMap<Long, Double> sortedMap = new LinkedHashMap<Long, Double>(); for (Entry<Long, Double> entrySet : mapList) { sortedMap.put(entrySet.getKey(), entrySet.getValue()); } return sortedMap; } }