*/
Value Sort Map Utility (Java Code)
Submitted By:
nirajagarwal
Rating:
(Not rated) (
Rate It)
This class is used to show how you can sort a java.uti.Map for values. This also takes care of null and duplicate values present in the map. Many times we come accross the requirement where we need to sort a Map (HashMap, Hashtable) for it's values. Like User Id's as key and User Name as values; and need to sort it with User Names. This can be good fit for such scenarios.
File Details
NOTE: Some downloads must be obtained through publishers´s site.
Do you want to get your software listed on this site? Go to our
submissions area.
Screenshot
Details
Comments (2)
Comment
Posted by: anil on Friday, June 22, 2007
it is not working when i give numbers as values. its not sorting..but good for the strings
sortByValue(Map map)
Posted by: traveler_82@poczta.onet.pl on Tuesday, July 03, 2007
static Map sortByValue(Map map) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
// logger.info(list);
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
Add Your Rating