Love this site? Hate it? Leave us some comments.

Value Sort Map Utility (Java Code)

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

No Image

Details

File Name:
Unknown
Product homepage:
Order page:
Languages:
Unknown
Platforms:
Unknown
Release date:
1/1/1900
License:
Unknown
Size:
Unknown
Price/Fee (US$):
Unknown
Number of downloads:
636

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

(Not published)
star
starstar
starstarstar
starstarstarstar
starstarstarstarstar

corner
© 1996-2008. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin.
bootstrapLabs Logo A bootstrapLabs project.