Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sorting hashmap based on values

 
Ranch Hand
Posts: 156
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to sort hashmap based on values and not keys.. one more concern here is the values are duplicate..
the hash map has data like

HashMap hashMap=new HashMap();

hashMap.put("aaa",new Integer("333"));
hashMap.put("bbb",new Integer("111"));
hashMap.put("ccc",new Integer("444"));
hashMap.put("eee",new Integer("222"));
hashMap.put("ggg",new Integer("333"));

after sorting the map shuld have the data like

hashMap.put("bbb",new Integer("111"));
hashMap.put("eee",new Integer("222"));
hashMap.put("aaa",new Integer("333"));
hashMap.put("ggg",new Integer("333"));
hashMap.put("ccc",new Integer("444"));

can you please help me in solving this...
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
come from www.theserverside.com/discussions/thread.tss?thread_id=29569. But the link break I used cached page from google. You can google search to find it.



Map someMap= new HashMap();

someMap.put("key1","c");
someMap.put("key2","a");
someMap.put("key3","b");

List mapKeys = new ArrayList(someMap.keySet());
List mapValues = new ArrayList(someMap.values());

someMap.clear();

TreeSet sortedSet = new TreeSet(mapValues);

Object[] sortedArray = sortedSet.toArray();

int size = sortedArray.length;

// a) Ascending sort

for (int i=0; i<size; i++)
{

// System.out.println(sortedArray[i]);

someMap.put(mapKeys.get(mapValues.indexOf(sortedArray[i])), sortedArray[i]);

}

System.out.println(someMap);

someMap.clear();

// b) Descending sort

for (int i=size; i>0
{

someMap.put(mapKeys.get(mapValues.indexOf(sortedArray[--i])), sortedArray[i]);

}

System.out.println(someMap);

 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also use a SortedSet<Entry.Map<String,Integer>> instad of a Map.

Then create a Comparator<Map.Entry<String,Integer>> and use it in the TreeMap to get your items ordered according to the value and not the key.
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic