By Abe

2008-09-20 21:03:23 8 Comments

I am relatively new to Java, and often find that I need to sort a Map<Key, Value> on the values.

Since the values are not unique, I find myself converting the keySet into an array, and sorting that array through array sort with a custom comparator that sorts on the value associated with the key.

Is there an easier way?


@user_3380739 2016-11-28 20:16:27

Here is the code by Java 8 with AbacusUtil

Map<String, Integer> map = N.asMap("a", 2, "b", 3, "c", 1, "d", 2);
Map<String, Integer> sortedMap = Stream.of(map.entrySet()).sorted(Map.Entry.comparingByValue()).toMap(e -> e.getKey(), e -> e.getValue(),
// output: {c=1, a=2, d=2, b=3}

Declaration´╝Ü I'm the developer of AbacusUtil.

@AjahnCharles 2017-08-19 17:13:32

What part of the answer is using AbacusUtil? Just the toMap() helper?

@parsecer 2018-10-07 13:13:25

The simplest brute-force sortHashMap method for HashMap<String, Long>: you can just copypaste it and use like this:

public class Test  {
    public static void main(String[] args)  {
        HashMap<String, Long> hashMap = new HashMap<>();
        hashMap.put("Cat", (long) 4);
        hashMap.put("Human", (long) 2);
        hashMap.put("Dog", (long) 4);
        hashMap.put("Fish", (long) 0);
        hashMap.put("Tree", (long) 1);
        hashMap.put("Three-legged-human", (long) 3);
        hashMap.put("Monkey", (long) 2);

        System.out.println(hashMap);  //{Human=2, Cat=4, Three-legged-human=3, Monkey=2, Fish=0, Tree=1, Dog=4}
        System.out.println(sortHashMap(hashMap));  //{Cat=4, Dog=4, Three-legged-human=3, Human=2, Monkey=2, Tree=1, Fish=0}

    public LinkedHashMap<String, Long> sortHashMap(HashMap<String, Long> unsortedMap)  {
        LinkedHashMap<String, Long> result = new LinkedHashMap<>();

        //add String keys to an array: the array would get sorted, based on those keys' values
        ArrayList<String> sortedKeys = new ArrayList<>();
        for (String key: unsortedMap.keySet())  {

        //sort the ArrayList<String> of keys    
        for (int i=0; i<unsortedMap.size(); i++)  {
            for (int j=1; j<sortedKeys.size(); j++)  {
                if (unsortedMap.get(sortedKeys.get(j)) > unsortedMap.get(sortedKeys.get(j-1))) {
                    String temp = sortedKeys.get(j);
                    sortedKeys.set(j, sortedKeys.get(j-1));
                    sortedKeys.set(j-1, temp);

        // construct the result Map
        for (String key: sortedKeys)  {
            result.put(key, unsortedMap.get(key));

        return result;

@assylias 2014-03-02 19:38:15

With Java 8, you can use the streams api to do it in a significantly less verbose way:

Map<K, V> sortedMap = map.entrySet().stream()
                         .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

@Vlad Holubiev 2014-06-01 19:18:17

How to sort it in a reverse order?

@assylias 2014-06-01 19:33:21

@VladGolubev comparing(Entry::getValue).reversed()

@Vlad Holubiev 2014-08-09 08:26:56

found a solution - Collections.reverseOrder(comparing(Entry::getValue))

@Vlad Holubiev 2014-08-09 08:49:57

@Jake Stokes 2017-10-02 14:31:13

I think I see a typo there -- shouldn't the " toMap " be called as " Collectors.toMap() " ?

@assylias 2017-10-03 05:59:20

@JakeStokes Or use a static import :-)

@Gediminas Rimsa 2018-11-24 09:20:39

A better way to sort by entry value in reverse order is: Entry.comparingByValue(Comparator.reverseOrder())

@Kenston Choi 2018-07-21 14:56:50

If there is a preference of having a Map data structure that inherently sorts by values without having to trigger any sort methods or explicitly pass to a utility, then the following solutions may be applicable:

(1) org.drools.chance.core.util.ValueSortedMap (JBoss project) maintains two maps internally one for lookup and one for maintaining the sorted values. Quite similar to previously added answers, but probably it is the abstraction and encapsulation part (including copying mechanism) that makes it safer to use from the outside.

(2) avoids maintaining two maps and instead relies/extends from Apache Common's LinkedMap. (Blog author's note: as all the code here is in the public domain):

// required to access LinkEntry.before and LinkEntry.after

// SNIP: imports

* map implementation based on LinkedMap that maintains a sorted list of
* values for iteration
public class ValueSortedHashMap extends LinkedMap {
    private final boolean _asc;

    // don't use super()!
    public ValueSortedHashMap(final boolean asc) {
        _asc = asc;

    // SNIP: some more constructors with initial capacity and the like

    protected void addEntry(final HashEntry entry, final int hashIndex) {
        final LinkEntry link = (LinkEntry) entry;
        data[hashIndex] = entry;

    protected void updateEntry(final HashEntry entry, final Object newValue) {
        final LinkEntry link = (LinkEntry) entry;
        link.before.after = link.after;
        link.after.before = link.before;
        link.after = link.before = null;

    private void insertSorted(final LinkEntry link) {
        LinkEntry cur = header;
        // iterate whole list, could (should?) be replaced with quicksearch
        // start at end to optimize speed for in-order insertions
        while ((cur = cur.before) != header & amp; & amp; !insertAfter(cur, link)) {}
        link.after = cur.after;
        link.before = cur;
        cur.after.before = link;
        cur.after = link;

    protected boolean insertAfter(final LinkEntry cur, final LinkEntry link) {
        if (_asc) {
            return ((Comparable) cur.getValue())
            .compareTo((V) link.getValue()) & lt; = 0;
        } else {
            return ((Comparable) cur.getValue())
            .compareTo((V) link.getValue()) & gt; = 0;

    public boolean isAscending() {
        return _asc;

(3) Write a custom Map or extends from LinkedHashMap that will only sort during enumeration (e.g., values(), keyset(), entryset()) as needed. The inner implementation/behavior is abstracted from the one using this class but it appears to the client of this class that values are always sorted when requested for enumeration. This class hopes that sorting will happen mostly once if all put operations have been completed before enumerations. Sorting method adopts some of the previous answers to this question.

public class SortByValueMap<K, V> implements Map<K, V> {

    private boolean isSortingNeeded = false;

    private final Map<K, V> map = new LinkedHashMap<>();

    public V put(K key, V value) {
        isSortingNeeded = true;
        return map.put(key, value);

    public void putAll(Map<? extends K, ? extends V> map) {
        isSortingNeeded = true;

    public Set<K> keySet() {
        return map.keySet();

    public Set<Entry<K, V>> entrySet() {
        return map.entrySet();

    public Collection<V> values() {
        return map.values();

    private void sort() {
        if (!isSortingNeeded) {

        List<Entry<K, V>> list = new ArrayList<>(size());

        for (Iterator<Map.Entry<K, V>> it = map.entrySet().iterator(); it.hasNext();) {
            Map.Entry<K, V> entry =;


        for (Entry<K, V> entry : list) {
            map.put(entry.getKey(), entry.getValue());

        isSortingNeeded = false;

    public String toString() {
        return map.toString();

(4) Guava offers ImmutableMap.Builder.orderEntriesByValue(Comparator valueComparator) although the resulting map will be immutable:

Configures this Builder to order entries by value according to the specified comparator.

The sort order is stable, that is, if two entries have values that compare as equivalent, the entry that was inserted first will be first in the built map's iteration order.

@smart-developer 2018-08-22 16:15:06

I rewrote devinmoore's method that performs sorting a map by it's value without using Iterator :

public static Map<K, V> sortMapByValue(Map<K, V> inputMap) {

    Set<Entry<K, V>> set = inputMap.entrySet();
    List<Entry<K, V>> list = new ArrayList<Entry<K, V>>(set);

    Collections.sort(list, new Comparator<Map.Entry<K, V>>()
        public int compare(Entry<K, V> o1, Entry<K, V> o2) {
            return (o1.getValue()).compareTo( o2.getValue() );  //Ascending order
    } );

    Map<K, V> sortedMap = new LinkedHashMap<>();

    for(Map.Entry<K, V> entry : list){
        sortedMap.put(entry.getKey(), entry.getValue());

    return sortedMap;

Note: that we used LinkedHashMap as output map, because our list has been sorted by value and now we should store our list into output map with order of inserted key,values. So if you use for example TreeMap as your output map, your map will be sorted by map keys again!

This is the main method:

public static void main(String[] args) {
    Map<String, String> map = new HashMap<>();
    map.put("3", "three");
    map.put("1", "one");
    map.put("5", "five");
    System.out.println("Input Map:" + map);
    System.out.println("Sorted Map:" + sortMapByValue(map));

Finally, this is the output:

Input Map:{1=one, 3=three, 5=five}
Sorted Map:{5=five, 1=one, 3=three}

@Pankaj Singhal 2018-05-18 07:40:39

Late Entry.

With the advent of Java-8, we can use streams for data manipulation in a very easy/succinct way. You can use streams to sort the map entries by value and create a LinkedHashMap which preserves insertion-order iteration.


LinkedHashMap sortedByValueMap = map.entrySet().stream()
                .sorted(comparing(Entry<Key,Value>::getValue).thenComparing(Entry::getKey))     //first sorting by Value, then sorting by Key(entries with same value)
                .collect(LinkedHashMap::new,(map,entry) -> map.put(entry.getKey(),entry.getValue()),LinkedHashMap::putAll);

For reverse ordering, replace:




@Pedi T. 2019-01-11 08:58:24

Thanks for this commented version. One question: What is the difference of using Entry.comparingByValue()(as assylias answer above or comparing(Entry<Key,Value>::getValue).thenComparing(Entry::g‌​etKey) that you used? I understand you also compare keys if values are identical, right? I noticed that the sorting keeps order of elements with the same value - so is sorting by keys necessary if the keys happen to be already sorted before?

@Carter Page 2010-04-05 23:24:05

Here's a generic-friendly version:

public class MapUtil {
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
        List<Entry<K, V>> list = new ArrayList<>(map.entrySet());

        Map<K, V> result = new LinkedHashMap<>();
        for (Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());

        return result;

@ciamej 2012-05-30 00:50:32

why use Collections.sort if there is Arrays.sort? see my answer for more details

@John Mark 2012-06-19 16:06:04

This saved me a TON of time! The test case is great too. :) Is there a reason why you used a LinkedHashMap as opposed to a regular HashMap?

@Carter Page 2012-07-01 12:46:16

Glad this helps. John, the LinkedHashMap is important to the solution as it provides predictable iteration order.

@buzz3791 2013-04-25 15:02:29

Note this solution would not work reliably on a map which contains mutable values e.g. AtomicLong. I wanted to use it for a performance statistics collector in our application. So first needed to snapshot all the AtomicLong values and then sort a map with the snapshots.

@Carter Page 2013-04-25 18:55:17

@buzz3791 True. That's going to be the case in any sorting algorithm. Change the value of nodes in a structure during a sort creates unpredictable (and nearly always bad) results.

@Fire in the Hole 2014-11-01 07:53:22

DOESN'T WORK, I tried your code in android, it sorts the hashmap by value but after that when it iterates to fill the result Map it gets sorted again by key

@saiyancoder 2014-12-08 01:12:29

@Sheagorath I tried it in Android and it works too. It is not a platform specific problem, considering you are using the Java 6 version. Have you implemented Comparable correctly in your value object?

@anton1980 2015-02-02 22:29:54

This solution did not work for me. I am using BigDecimal as my value object and Pair<K, V> for the key (from Apache Commons). The values are not sorted when I access the map after sorting.

@rob 2015-06-27 14:06:30

Shouldn't the Java 8 version use forEachOrdered instead of forEach, since the docs of forEach states: "The behavior of this operation is explicitly nondeterministic."?

@Nathan Beach 2015-09-02 03:07:48

totally ripped this, but credited @CarterPage in the comments (it'll be in an open source project anyway). thanks so much.

@i_am_zero 2015-09-06 11:55:34

Any specific reason for using LinkedList and LinkedHashMap?

@Solomonoff's Secret 2015-09-30 14:53:00

@akhil_mittal LinkedList should be replaced by ArrayList but LinkedHashMap is necessary because it preserves insertion order.

@cirko 2015-11-13 12:40:40

@trillions Could you tell me what change you made to reverse the order? Thanks.

@Ferrybig 2015-11-17 11:26:30

+1, but the junit test is not the best, based on randomness it could fail or succeed without changes in the code

@Alexis C. 2015-12-20 21:25:28

The Java 8 version should use the collectors approach... Look at Collectors.toMap.

@Dake 2016-08-26 11:02:03

How are identical values sorted with code?

@basZero 2016-10-04 15:33:15

The Java 8 version only compiles in my Eclipse environment if I replace LinkedHashMap::new by LinkedHashMap<K, V>::new

@emilly 2016-11-28 14:50:34

I think LinkedList should be replaced with ArrayList as we just want to itertate over list .

@R. Oosterholt 2017-05-05 08:20:59

You might want to change for(int i = 0 ; i < 1000 ; ++i) { by while (testMap.size() < 1000) { in the unit test because same key might be generated by random. This might fails the testMap.size() assertion.

@weston 2017-05-15 16:35:16

Not loving the test that uses different data every time new Random(System.currentTimeMillis())

@Lii 2018-03-10 22:14:30

I removed all solutions except the original one. The others should get their own answers instead.

@Arun Raaj 2018-01-02 22:35:37

public class Test {
  public static void main(String[] args) {
    TreeMap<Integer, String> hm=new TreeMap();
    hm.put(3, "arun singh");
    hm.put(5, "vinay singh");
    hm.put(1, "bandagi singh");
    hm.put(6, "vikram singh");
    hm.put(2, "panipat singh");
    hm.put(28, "jakarta singh");

    ArrayList<String> al=new ArrayList(hm.values());
    Collections.sort(al, new myComparator());

    System.out.println("//sort by values \n");
    for(String obj: al){
        for(Map.Entry<Integer, String> map2:hm.entrySet()){
                System.out.println(map2.getKey()+" "+map2.getValue());

class myComparator implements Comparator{
    public int compare(Object o1, Object o2) {
       String o3=(String) o1;
       String o4 =(String) o2;
       return o3.compareTo(o4);


//sort by values 

3 arun singh
1 bandagi singh
28 jakarta singh
2 panipat singh
6 vikram singh
5 vinay singh

@Stephen C 2018-10-07 01:05:06

O(N^2) solution. And it will produce spurious results if there are duplicate values.

@Brian Goetz 2014-05-24 15:53:51

Java 8 offers a new answer: convert the entries into a stream, and use the comparator combinators from Map.Entry:

Stream<Map.Entry<K,V>> sorted =

This will let you consume the entries sorted in ascending order of value. If you want descending value, simply reverse the comparator:

Stream<Map.Entry<K,V>> sorted =

If the values are not comparable, you can pass an explicit comparator:

Stream<Map.Entry<K,V>> sorted =

You can then proceed to use other stream operations to consume the data. For example, if you want the top 10 in a new map:

Map<K,V> topTen =
          Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

Or print to System.out:


@Benj 2014-12-09 18:21:54

Nice, but what about the use of parallelStream() in this case ?

@Brian Goetz 2014-12-09 18:37:25

It will work in parallel, however, you may find that the cost of merging maps to combine the partial results is too expensive and the parallel version may not perform as well as you'd hope. But it does work and produce the correct answer.

@Benj 2014-12-10 19:30:49

Thanks for your useful advice. It was exactly what I was wondering, though it depends on what type of key you use and so many parameters... The important thing is "it does work and produce the correct answer".

@Pete 2015-04-28 15:04:26

How can I sort according to the values being Lists with different sizes? I want to sort by descending list size.

@CyberMew 2015-10-06 09:18:13

Once sorted how would we go about extracting back the map from the Stream<>?

@Leo 2016-08-19 23:34:01

don' t you have to use compareByValue in the top10 example?

@chandresh 2017-07-04 06:25:20

How do I copy the sorted values of the map in a list?

@Steven 2017-08-21 11:19:11

The part for making up a top ten is incorrect, you need to add two more parameters as posted here:

@Vadzim 2017-11-21 00:28:45

Collectors.toMap here would default to plain HashMap, which would discard the ordering. Refer to the top answer for properly collecting to LinkedHashMap.

@shmosel 2017-11-21 00:47:40

@Vadzim The point was to capture the top 10, not to preserve the order.

@Vadzim 2017-11-21 01:50:34

@shmosel, the order makes sense even for top 10 ;)

@shmosel 2017-11-21 01:51:48

@Vadzim Not sure what you're trying to say.

@David Bleckmann 2016-04-24 05:11:41

There are a lot of answers for this question already, but none provided me what I was looking for, a map implementation that returns keys and entries sorted by the associated value, and maintains this property as keys and values are modified in the map. Two other questions ask for this specifically.

I cooked up a generic friendly example that solves this use case. This implementation does not honor all of the contracts of the Map interface, such as reflecting value changes and removals in the sets return from keySet() and entrySet() in the original object. I felt such a solution would be too large to include in a Stack Overflow answer. If I manage to create a more complete implementation, perhaps I will post it to Github and then to it link in an updated version of this answer.

import java.util.*;

 * A map where {@link #keySet()} and {@link #entrySet()} return sets ordered
 * by associated values based on the the comparator provided at construction
 * time. The order of two or more keys with identical values is not defined.
 * <p>
 * Several contracts of the Map interface are not satisfied by this minimal
 * implementation.
public class ValueSortedMap<K, V> extends HashMap<K, V> {
    protected Map<V, Collection<K>> valueToKeysMap;

    // uses natural order of value object, if any
    public ValueSortedMap() {
        this((Comparator<? super V>) null);

    public ValueSortedMap(Comparator<? super V> valueComparator) {
        this.valueToKeysMap = new TreeMap<V, Collection<K>>(valueComparator);

    public boolean containsValue(Object o) {
        return valueToKeysMap.containsKey(o);

    public V put(K k, V v) {
        V oldV = null;
        if (containsKey(k)) {
            oldV = get(k);
        super.put(k, v);
        if (!valueToKeysMap.containsKey(v)) {
            Collection<K> keys = new ArrayList<K>();
            valueToKeysMap.put(v, keys);
        } else {
        return oldV;

    public void putAll(Map<? extends K, ? extends V> m) {
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
            put(e.getKey(), e.getValue());

    public V remove(Object k) {
        V oldV = null;
        if (containsKey(k)) {
            oldV = get(k);
        return oldV;

    public void clear() {

    public Set<K> keySet() {
        LinkedHashSet<K> ret = new LinkedHashSet<K>(size());
        for (V v : valueToKeysMap.keySet()) {
            Collection<K> keys = valueToKeysMap.get(v);
        return ret;

    public Set<Map.Entry<K, V>> entrySet() {
        LinkedHashSet<Map.Entry<K, V>> ret = new LinkedHashSet<Map.Entry<K, V>>(size());
        for (Collection<K> keys : valueToKeysMap.values()) {
            for (final K k : keys) {
                final V v = get(k);
                ret.add(new Map.Entry<K,V>() {
                    public K getKey() {
                        return k;

                    public V getValue() {
                        return v;

                    public V setValue(V v) {
                        throw new UnsupportedOperationException();
        return ret;

@Tanuj Verma 2017-02-20 05:37:33

Best thing is to convert HashMap to TreeMap. TreeMap sort keys on its own. If you want to sort on values than quick fix can be you can switch values with keys if your values are not duplicates.

@Nilesh Jadav 2015-11-19 10:29:01

Best Approach

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry; 

public class OrderByValue {

  public static void main(String a[]){
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("java", 20);
    map.put("C++", 45);
    map.put("Unix", 67);
    map.put("MAC", 26);
    map.put("Why this kolavari", 93);
    Set<Entry<String, Integer>> set = map.entrySet();
    List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
    Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
        public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
            return (o1.getValue()).compareTo( o2.getValue() );//Ascending order
            //return (o2.getValue()).compareTo( o1.getValue() );//Descending order
    } );
    for(Map.Entry<String, Integer> entry:list){
        System.out.println(entry.getKey()+" ==== "+entry.getValue());


java ==== 20

MAC ==== 26

C++ ==== 45

Unix ==== 67

Why this kolavari ==== 93

@Alexander 2016-09-23 11:10:50

My solution is a quite simple approach in the way of using mostly given APIs. We use the feature of Map to export its content as Set via entrySet() method. We now have a Set containing Map.Entry objects.

Okay, a Set does not carry an order, but we can take the content an put it into an ArrayList. It now has an random order, but we will sort it anyway.

As ArrayList is a Collection, we now use the Collections.sort() method to bring order to chaos. Because our Map.Entry objects do not realize the kind of comparison we need, we provide a custom Comparator.

public static void main(String[] args) {
    HashMap<String, String> map = new HashMap<>();
    map.put("Z", "E");
    map.put("G", "A");
    map.put("D", "C");
    map.put("E", null);
    map.put("O", "C");
    map.put("L", "D");
    map.put("Q", "B");
    map.put("A", "F");
    map.put(null, "X");
    MapEntryComparator mapEntryComparator = new MapEntryComparator();

    List<Entry<String,String>> entryList = new ArrayList<>(map.entrySet());
    Collections.sort(entryList, mapEntryComparator);

    for (Entry<String, String> entry : entryList) {
        System.out.println(entry.getKey() + " : " + entry.getValue());


@devinmoore 2008-09-20 21:06:34


private static <K, V> Map<K, V> sortByValue(Map<K, V> map) {
    List<Entry<K, V>> list = new LinkedList<>(map.entrySet());
    Collections.sort(list, new Comparator<Object>() {
        public int compare(Object o1, Object o2) {
            return ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue()).compareTo(((Map.Entry<K, V>) (o2)).getValue());

    Map<K, V> result = new LinkedHashMap<>();
    for (Iterator<Entry<K, V>> it = list.iterator(); it.hasNext();) {
        Map.Entry<K, V> entry = (Map.Entry<K, V>);
        result.put(entry.getKey(), entry.getValue());

    return result;

@Dimitris Andreou 2010-04-08 13:13:35

The list to be sorted is "new LinkedList"?? Gee. Thankfully Collections.sort() dump the list to an array first, to avoid precisely this kind of error (but still, dumping an ArrayList to an array should be faster than doing the same for a LinkedList).

@lisak 2011-06-03 16:31:15

cannot convert from Iterator to TernaryTree.Iterator

@ciamej 2012-05-30 00:49:53

why use Collections.sort if there is Arrays.sort? see my answer for more details

@kaspersky 2012-11-28 11:15:35

@DimitrisAndreou, why is it bad to sort a linkedlist?

@Dimitris Andreou 2012-11-29 19:29:47

@gg.kaspersky I'm not saying "it's bad to sort a LinkedList", but that LinkedList itself is a bad choice here, regardless of sorting. Much better to use an ArrayList, and for extra points, size it at exactly map.size(). Also see… average cost per element in ArrayList: 5 bytes average cost per element in LinkedList: 24 bytes. For an exactly sized ArrayList, the average cost would be 4 bytes. That is, LinkedList takes SIX times the amount of memory that ArrayList needs. It's just bloat

@Rishi Dua 2014-06-26 03:33:50

Shows an error: The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (List, new Comparator(){})

@Rishi Dua 2014-06-26 03:34:31

^Sorry about that. I didn't import java.util.comparator

@ram 2015-04-16 13:09:58

using above values has been sorted in ascending order. How to sort in descending ?

@Soheil 2017-06-05 20:14:11

Replace o1 and o2 to sort descending.

@user157196 2009-08-16 07:53:12

Important note:

This code can break in multiple ways. If you intend to use the code provided, be sure to read the comments as well to be aware of the implications. For example, values can no longer be retrieved by their key. (get always returns null.)

It seems much easier than all of the foregoing. Use a TreeMap as follows:

public class Testing {
    public static void main(String[] args) {
        HashMap<String, Double> map = new HashMap<String, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);

        map.put("A", 99.5);
        map.put("B", 67.4);
        map.put("C", 67.4);
        map.put("D", 67.3);

        System.out.println("unsorted map: " + map);
        System.out.println("results: " + sorted_map);

class ValueComparator implements Comparator<String> {
    Map<String, Double> base;

    public ValueComparator(Map<String, Double> base) {
        this.base = base;

    // Note: this comparator imposes orderings that are inconsistent with
    // equals.
    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        } // returning 0 would merge keys


unsorted map: {D=67.3, A=99.5, B=67.4, C=67.4}
results: {D=67.3, B=67.4, C=67.4, A=99.5}

@Stephen 2010-08-11 21:50:54

Not any more (…). Also, why was there a cast to Double? Shouldn't it just be return ((Comparable)base.get(a).compareTo(((Comparable)base.get(b))‌​)?

@steffen 2010-08-20 07:00:23

@Stephen: No. In this case all keys equal by value are dropped (difference between equals and comparion by reference). Additionally: Even this code has problems with the following sequence map.put("A","1d");map.put("B","1d");map.put("C",67d);map.put‌​("D",99.5d);

@mR_fr0g 2010-12-01 14:36:50

The comparator used for the treemap is inconsistent with equals (see the sortMap javadox). This means retireving items from the tree map will not work. sorted_map.get("A") will return null. That means this use of treemap is broken.

@aalku 2011-11-05 16:16:00

@mR_fr0g That is true only if the base map is altered. As long as it is not modified the natural order of the keys (according to the comparator) will follow the natural order of the values.

@Maxy-B 2011-11-24 04:37:55

Just in case it's not clear to people: this solution will probably not do what you want if you have multiple keys mapping to the same value -- only one of those keys will appear in the sorted result.

@lisak 2012-05-23 14:37:58

It's practically a bug ... I can image a very few use cases for omitting one of 2 map entries that have the same value...

@haylem 2012-07-03 21:19:30

Louis Wasserman (yes, one of the Google Guava guys), actually dislikes this answer quite a bit: "It breaks in several really confusing ways if you even look at it funny. If the backing map changes, it will break. If multiple keys map to the same value, it will break. If you call get on a key that isn't in the backing map, it will break. If you do anything whatsoever that would cause a lookup to happen on a key that isn't in the map -- a Map.equals call, containsKey, anything -- it will break with really weird stack traces."

@Tomasz 2012-09-06 17:14:42

If ValueComparator#compare() does not return 0 for equals (see edited answer) @Maxy-B 's comment about missing keys no longer applies

@Volodymyr Krupach 2013-04-08 15:58:34

I regret using this approach. Now I reproduced in my code all the bugs described here in comments. My mistake was not reading the comments. This works just fine.

@Konrad Morawski 2013-09-11 22:05:49

var sorted_map = map.OrderByDescending(entry => entry.Value); (in C#). Sorry, I couldn't resist ;) +1, though

@Mohammad 2014-03-11 09:05:41

I think adding if(a.equals(b)) return 0; in compare will make it perfect. As it will eliminate duplicate keys + will make normal traversal possible. Let me know if I am missing something, I am going to give it a chance in production...:P

@Aniket Kapse 2015-04-13 12:54:12

Can't we just use a TreeMap instead? Did I miss anything?

@Victor 2015-08-22 15:57:07

@AniketKapse, you can use only a tree map constructed after the map is populated (There is no need ValueComparator). Then you can iterate through the map following an order.

@Bruce Zu 2016-01-14 19:28:07

@Override public int compare(Character o1, Character o2) { if (charToTimes.get(o1) == null || charToTimes.get(o2) == null) { return -o1.compareTo(o2); } int result = -charToTimes.get(o1).compareTo(charToTimes.get(o2)); if (result != 0) { return result; } return -o1.compareTo(o2);}

@hephestos 2018-10-09 08:44:43

Don'y you compare objects and not primitive values...thus don't you compare memory position instead real values ?

@Bruce Zu 2016-01-15 01:09:37

    static <K extends Comparable<? super K>, V extends Comparable<? super V>>
    Map sortByValueInDescendingOrder(final Map<K, V> map) {
        Map re = new TreeMap(new Comparator<K>() {
            public int compare(K o1, K o2) {
                if (map.get(o1) == null || map.get(o2) == null) {
                    return -o1.compareTo(o2);
                int result = -map.get(o1).compareTo(map.get(o2));
                if (result != 0) {
                    return result;
                return -o1.compareTo(o2);
        return re;
    @Test(timeout = 3000l, expected = Test.None.class)
    public void testSortByValueInDescendingOrder() {
        char[] arr = "googler".toCharArray();
        Map<Character, Integer> charToTimes = new HashMap();
        for (int i = 0; i < arr.length; i++) {
            Integer times = charToTimes.get(arr[i]);
            charToTimes.put(arr[i], times == null ? 1 : times + 1);
        Map sortedByTimes = sortByValueInDescendingOrder(charToTimes);
        Assert.assertEquals(charToTimes.toString(), "{g=2, e=1, r=1, o=2, l=1}");
        Assert.assertEquals(sortedByTimes.toString(), "{o=2, g=2, r=1, l=1, e=1}");
        Assert.assertEquals(sortedByTimes.containsKey('a'), false);
        Assert.assertEquals(sortedByTimes.get('a'), null);
        Assert.assertEquals(sortedByTimes.get('g'), 2);
        Assert.assertEquals(sortedByTimes.equals(charToTimes), true);

@Uxio 2015-12-15 17:26:17

If there's not any value bigger than the size of the map, you could use arrays, this should be the fastest approach:

public List<String> getList(Map<String, Integer> myMap) {
    String[] copyArray = new String[myMap.size()];
    for (Entry<String, Integer> entry : myMap.entrySet()) {
        copyArray[entry.getValue()] = entry.getKey();
    return Arrays.asList(copyArray);

@Tom 2015-12-15 21:57:17

This copyArray[entry.getValue()] is very error-prone, since it will fail, if the map contains values which are larger than the map size.

@Vitalii Fedorenko 2013-07-28 02:26:00

You can try Guava's multimaps:

TreeMap<Integer, Collection<String>> sortedMap = new TreeMap<>(
        ArrayListMultimap.<Integer, String>create()).asMap());

As a result you get a map from original values to collections of keys that correspond to them. This approach can be used even if there are multiple keys for the same value.

@RoyalBigorno 2011-07-05 14:49:19

Use a generic comparator such as :

final class MapValueComparator<K,V extends Comparable<V>> implements Comparator<K> {

    private Map<K,V> map;

    private MapValueComparator() {

    public MapValueComparator(Map<K,V> map) {
        this(); = map;

    public int compare(K o1, K o2) {
        return map.get(o1).compareTo(map.get(o2));

@gdejohn 2013-11-20 06:54:39

To accomplish this with the new features in Java 8:

import static java.util.Map.Entry.comparingByValue;
import static;

<K, V> List<Entry<K, V>> sort(Map<K, V> map, Comparator<? super V> comparator) {
    return map.entrySet().stream().sorted(comparingByValue(comparator)).collect(toList());

The entries are ordered by their values using the given comparator. Alternatively, if your values are mutually comparable, no explicit comparator is needed:

<K, V extends Comparable<? super V>> List<Entry<K, V>> sort(Map<K, V> map) {
    return map.entrySet().stream().sorted(comparingByValue()).collect(toList());

The returned list is a snapshot of the given map at the time this method is called, so neither will reflect subsequent changes to the other. For a live iterable view of the map:

<K, V extends Comparable<? super V>> Iterable<Entry<K, V>> sort(Map<K, V> map) {
    return () -> map.entrySet().stream().sorted(comparingByValue()).iterator();

The returned iterable creates a fresh snapshot of the given map each time it's iterated, so barring concurrent modification, it will always reflect the current state of the map.

@assylias 2014-03-03 17:26:05

This returns a List of Entries rather than a map sorted by value. Other version that returns a map:

@RobotMan 2014-04-22 09:09:59

I've merged the solutions of user157196 and Carter Page:

class MapUtil {

    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue( Map<K, V> map ){
        ValueComparator<K,V> bvc =  new ValueComparator<K,V>(map);
        TreeMap<K,V> sorted_map = new TreeMap<K,V>(bvc);
        return sorted_map;


class ValueComparator<K, V extends Comparable<? super V>> implements Comparator<K> {

    Map<K, V> base;
    public ValueComparator(Map<K, V> base) {
        this.base = base;

    public int compare(K a, K b) {
        int result = (base.get(a).compareTo(base.get(b)));
        if (result == 0) result=1;
        // returning 0 would merge keys
        return result;

@rohan kamat 2013-10-24 10:13:12

as map is unordered to sort it ,we can do following

Map<String, String> map= new TreeMap<String, String>(unsortMap);

You should note that, unlike a hash map, a tree map guarantees that its elements will be sorted in ascending key order.

@Duncan Jones 2014-04-15 07:29:24

This sorts based on keys, not values.

@Stephen 2010-08-06 03:59:41

Three 1-line answers...

I would use Google Collections Guava to do this - if your values are Comparable then you can use

valueComparator = Ordering.natural().onResultOf(Functions.forMap(map))

Which will create a function (object) for the map [that takes any of the keys as input, returning the respective value], and then apply natural (comparable) ordering to them [the values].

If they're not comparable, then you'll need to do something along the lines of

valueComparator = Ordering.from(comparator).onResultOf(Functions.forMap(map)) 

These may be applied to a TreeMap (as Ordering extends Comparator), or a LinkedHashMap after some sorting

NB: If you are going to use a TreeMap, remember that if a comparison == 0, then the item is already in the list (which will happen if you have multiple values that compare the same). To alleviate this, you could add your key to the comparator like so (presuming that your keys and values are Comparable):

valueComparator = Ordering.natural().onResultOf(Functions.forMap(map)).compound(Ordering.natural())

= Apply natural ordering to the value mapped by the key, and compound that with the natural ordering of the key

Note that this will still not work if your keys compare to 0, but this should be sufficient for most comparable items (as hashCode, equals and compareTo are often in sync...)

See Ordering.onResultOf() and Functions.forMap().


So now that we've got a comparator that does what we want, we need to get a result from it.

map = ImmutableSortedMap.copyOf(myOriginalMap, valueComparator);

Now this will most likely work work, but:

  1. needs to be done given a complete finished map
  2. Don't try the comparators above on a TreeMap; there's no point trying to compare an inserted key when it doesn't have a value until after the put, i.e., it will break really fast

Point 1 is a bit of a deal-breaker for me; google collections is incredibly lazy (which is good: you can do pretty much every operation in an instant; the real work is done when you start using the result), and this requires copying a whole map!

"Full" answer/Live sorted map by values

Don't worry though; if you were obsessed enough with having a "live" map sorted in this manner, you could solve not one but both(!) of the above issues with something crazy like the following:

Note: This has changed significantly in June 2012 - the previous code could never work: an internal HashMap is required to lookup the values without creating an infinite loop between the TreeMap.get() -> compare() and compare() -> get()

import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


class ValueComparableMap<K extends Comparable<K>,V> extends TreeMap<K,V> {
    //A map for doing lookups on the keys for comparison so we don't get infinite loops
    private final Map<K, V> valueMap;

    ValueComparableMap(final Ordering<? super V> partialValueOrdering) {
        this(partialValueOrdering, new HashMap<K,V>());

    private ValueComparableMap(Ordering<? super V> partialValueOrdering,
            HashMap<K, V> valueMap) {
        super(partialValueOrdering //Apply the value ordering
                .onResultOf(Functions.forMap(valueMap)) //On the result of getting the value for the key from the map
                .compound(Ordering.natural())); //as well as ensuring that the keys don't get clobbered
        this.valueMap = valueMap;

    public V put(K k, V v) {
        if (valueMap.containsKey(k)){
            //remove the key in the sorted set before adding the key again
        valueMap.put(k,v); //To get "real" unsorted values for the comparator
        return super.put(k, v); //Put it in value order

    public static void main(String[] args){
        TreeMap<String, Integer> map = new ValueComparableMap<String, Integer>(Ordering.natural());
        map.put("a", 5);
        map.put("b", 1);
        map.put("c", 3);
        //ensure it's still a map (by overwriting a key, but with a new value) 
        map.put("d", 2);
        assertEquals("b", map.firstKey());
        //Ensure multiple values do not clobber keys
        map.put("e", 2);
        assertEquals(5, map.size());
        assertEquals(2, (int) map.get("e"));
        assertEquals(2, (int) map.get("d"));

When we put, we ensure that the hash map has the value for the comparator, and then put to the TreeSet for sorting. But before that we check the hash map to see that the key is not actually a duplicate. Also, the comparator that we create will also include the key so that duplicate values don't delete the non-duplicate keys (due to == comparison). These 2 items are vital for ensuring the map contract is kept; if you think you don't want that, then you're almost at the point of reversing the map entirely (to Map<V,K>).

The constructor would need to be called as

 new ValueComparableMap(Ordering.natural());
 new ValueComparableMap(Ordering.from(comparator));

@smallufo 2010-11-10 10:58:52

Hi @Stephen , can you give an example how to use Ordering ? I look into Ordering's source code , and totally cannot figure out what .natural().onResultOf(...) returns! The source code is "public <F> Ordering<F> onResultOf" , I even don't know how it compiles ! Most important , how to use "<F> Ordering<F>" to sort a map ? Is it a comparator or something ? Thanks.

@Stephen 2010-11-11 11:44:54

Ordering is simply a rich Comparator. I've tried commenting each example (the italics underneath each one). "natural" indicates that the objects are Comparable; it's like apache common's ComparableComparator. onResultOf applies a function to the item being compared. So if you had a function that added 1 to an integer, then natural().onResultOf(add1Function).compare(1,2) would end up doing 2.compareTo(3)

@lbalazscs 2013-04-30 09:39:17

ImmutableSortedMap.copyOf throws IllegalArgumentException if there are duplicate values in the original map.

@Stephen 2013-05-01 00:31:18

@Ibalazscs Yes it will - You should be able to use ImmutableSetMultiMap or ImmutableListMultiMap to contain the collection of duplicate variables.

@alex 2013-08-21 09:22:37

Thanks for this, I used your solution in one project. I think there's a problem in put though: to behave like a map it needs to return the value previously associated with the key, if it exists, but like this it will never do. The solution I used is to return the removed value if it exists.

@Abdull 2014-04-22 09:41:49

Currently, class ValueComparableMap<K extends Comparable<K>,V> extends TreeMap<K,V>. Why K extends Comparable<K>? What about just K? Requiring the key type K to be a Comparable seems overly restrictive.

@Stephen 2014-04-22 18:07:20

@Abdull, you probably want to read the paragraph under the code a few times. (I've just re-read it and have had a little trouble a year later ;)). We have to ensure the keys are not clobbered (otherwise we could remove items with the same values). The easiest way to do this is to make the key comparable. So yes, it is restrictive, but you'd need to have 2 comparators otherwise (which is certainly possible, but outside the scope of the question).

@Christian Hujer 2015-03-05 22:47:36

Couldn't this simply work without the valueMap by using super.put() in the first place? Just thinking, haven't tried yet...

@Ryan The Leach 2015-06-10 02:39:53

Object valueComparator = Ordering.natural().onResultOf(Functions.forMap(map)).compoun‌​d(Ordering.natural()‌​); results in an error, Cannot resolve .compound(Ordering<Comparable>)

@has981 2016-06-05 07:56:43

Thanks for the solution, I had to add:@Override public boolean containsKey(Object key) { return valueMap.containsKey(key); } to overcome an exception thrown when calling containsKey.

@Sujan Reddy A 2013-02-10 06:10:09

Create customized comparator and use it while creating new TreeMap object.

class MyComparator implements Comparator<Object> {

    Map<String, Integer> map;

    public MyComparator(Map<String, Integer> map) { = map;

    public int compare(Object o1, Object o2) {

        if (map.get(o2) == map.get(o1))
            return 1;
            return ((Integer) map.get(o2)).compareTo((Integer)     


Use the below code in your main func

    Map<String, Integer> lMap = new HashMap<String, Integer>();
    lMap.put("A", 35);
    lMap.put("B", 75);
    lMap.put("C", 50);
    lMap.put("D", 50);

    MyComparator comparator = new MyComparator(lMap);

    Map<String, Integer> newMap = new TreeMap<String, Integer>(comparator);


{B=75, D=50, C=50, A=35}

@Sujan Reddy A 2013-02-10 06:12:12

This works for duplicate values too!!!

@gjgjgj 2019-03-17 17:57:49

In the case the values are equal, I changed the "return 1" line to compare the keys: "return ((String) o1).compareTo((String) o2);"

@cuneyt 2012-11-18 06:37:50

Major problem. If you use the first answer (Google takes you here), change the comparator to add an equal clause, otherwise you cannot get values from the sorted_map by keys:

public int compare(String a, String b) {
        if (base.get(a) > base.get(b)) {
            return 1;
        } else if (base.get(a) < base.get(b)){
            return -1;

        return 0;
        // returning 0 would merge keys

@Masood_mj 2013-01-23 04:54:00

Now when you add two entries with equal values they will be merged you should only return 0 if you are sure that the objects are the same (equal)

@Rashid C Y 2012-08-29 07:33:26

We simply sort a map just like this

            Map<String, String> unsortedMap = new HashMap<String, String>();

    unsortedMap.put("E", "E Val");
    unsortedMap.put("F", "F Val");
    unsortedMap.put("H", "H Val");
    unsortedMap.put("B", "B Val");
    unsortedMap.put("C", "C Val");
    unsortedMap.put("A", "A Val");
    unsortedMap.put("G", "G Val");
    unsortedMap.put("D", "D Val");

    Map<String, String> sortedMap = new TreeMap<String, String>(unsortedMap);

    System.out.println("\nAfter sorting..");
    for (Map.Entry <String, String> mapEntry : sortedMap.entrySet()) {
        System.out.println(mapEntry.getKey() + " \t" + mapEntry.getValue());

@NimChimpsky 2012-09-18 11:55:11

this just creates a treemap, treemaps sort on the key

@ciamej 2012-05-30 00:47:27

Instead of using Collections.sort as some do I'd suggest using Arrays.sort. Actually what Collections.sort does is something like this:

public static <T extends Comparable<? super T>> void sort(List<T> list) {
    Object[] a = list.toArray();
    ListIterator<T> i = list.listIterator();
    for (int j=0; j<a.length; j++) {;

It just calls toArray on the list and then uses Arrays.sort. This way all the map entries will be copied three times: once from the map to the temporary list (be it a LinkedList or ArrayList), then to the temporary array and finally to the new map.

My solution ommits this one step as it does not create unnecessary LinkedList. Here is the code, generic-friendly and performance-optimal:

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) 
    Map.Entry<K,V>[] array = map.entrySet().toArray(new Map.Entry[map.size()]);

    Arrays.sort(array, new Comparator<Map.Entry<K, V>>() 
        public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) 
            return e1.getValue().compareTo(e2.getValue());

    Map<K, V> result = new LinkedHashMap<K, V>();
    for (Map.Entry<K, V> entry : array)
        result.put(entry.getKey(), entry.getValue());

    return result;

@Anthony 2010-01-21 20:34:39

I've looked at the given answers, but a lot of them are more complicated than needed or remove map elements when several keys have same value.

Here is a solution that I think fits better:

public static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
    Comparator<K> valueComparator =  new Comparator<K>() {
        public int compare(K k1, K k2) {
            int compare = map.get(k2).compareTo(map.get(k1));
            if (compare == 0) return 1;
            else return compare;
    Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
    return sortedByValues;

Note that the map is sorted from the highest value to the lowest.

@Erel Segal-Halevi 2011-10-02 15:58:30

PROBLEM: if you want to use the returned map later, for example to check if it contains a certain element, you will always get false, because of your custom comparator! A possible solution: replace the last line with: return new LinkedHashMap<K,V>(sortedByValues);

@abhi 2013-05-14 08:14:40

This looks a clean solution to me , except the fact that @ErelSegalHalevi pointed out , checking whether the values exist in the Map will not be possible as you specified the comparator. map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("4", "Four"); map.put("5", "Five"); map.containsKey("1") will always return false , if you return new object in the function sortByValues() like return new TreeMap<K, V>(sortedByValues); solves the problem . Thanks Abhi

@Kirby 2014-04-22 17:37:16

pretty much the same as user157196's and Carter Page's answer. Carter Page's contains the LinkedHashMap fix

@cosmolev 2014-07-22 08:20:05

4th line of the solution should be int compare = map.get(k1).compareTo(map.get(k2)); if you need ascending order

@Darkless 2010-05-09 13:40:59

This is just too complicated. Maps were not supposed to do such job as sorting them by Value. The easiest way is to create your own Class so it fits your requirement.

In example lower you are supposed to add TreeMap a comparator at place where * is. But by java API it gives comparator only keys, not values. All of examples stated here is based on 2 Maps. One Hash and one new Tree. Which is odd.

The example:

Map<Driver driver, Float time> map = new TreeMap<Driver driver, Float time>(*);

So change the map into a set this way:

ResultComparator rc = new ResultComparator();
Set<Results> set = new TreeSet<Results>(rc);

You will create class Results,

public class Results {
    private Driver driver;
    private Float time;

    public Results(Driver driver, Float time) {
        this.driver = driver;
        this.time = time;

    public Float getTime() {
        return time;

    public void setTime(Float time) {
        this.time = time;

    public Driver getDriver() {
        return driver;

    public void setDriver (Driver driver) {
        this.driver = driver;

and the Comparator class:

public class ResultsComparator implements Comparator<Results> {
    public int compare(Results t, Results t1) {
        if (t.getTime() < t1.getTime()) {
            return 1;
        } else if (t.getTime() == t1.getTime()) {
            return 0;
        } else {
            return -1;

This way you can easily add more dependencies.

And as the last point I'll add simple iterator:

Iterator it = set.iterator();
while (it.hasNext()) {
    Results r = (Results);
    System.out.println( r.getDriver().toString
        //or whatever that is related to Driver class -getName() getSurname()
        + " "
        + r.getTime()

@Sebastien Lorber 2011-12-27 17:43:44

For sure the solution of Stephen is really great, but for those who can't use Guava:

Here's my solution for sorting by value a map. This solution handle the case where there are twice the same value etc...

// If you want to sort a map by value, and if there can be twice the same value:

// here is your original map
Map<String,Integer> mapToSortByValue = new HashMap<String, Integer>();
mapToSortByValue.put("A", 3);
mapToSortByValue.put("B", 1);
mapToSortByValue.put("C", 3);
mapToSortByValue.put("D", 5);
mapToSortByValue.put("E", -1);
mapToSortByValue.put("F", 1000);
mapToSortByValue.put("G", 79);
mapToSortByValue.put("H", 15);

// Sort all the map entries by value
Set<Map.Entry<String,Integer>> set = new TreeSet<Map.Entry<String,Integer>>(
        new Comparator<Map.Entry<String,Integer>>(){
            public int compare(Map.Entry<String,Integer> obj1, Map.Entry<String,Integer> obj2) {
                Integer val1 = obj1.getValue();
                Integer val2 = obj2.getValue();
                // DUPLICATE VALUE CASE
                // If the values are equals, we can't return 0 because the 2 entries would be considered
                // as equals and one of them would be deleted (because we use a set, no duplicate, remember!)
                int compareValues = val1.compareTo(val2);
                if ( compareValues == 0 ) {
                    String key1 = obj1.getKey();
                    String key2 = obj2.getKey();
                    int compareKeys = key1.compareTo(key2);
                    if ( compareKeys == 0 ) {
                        // what you return here will tell us if you keep REAL KEY-VALUE duplicates in your set
                        // if you want to, do whatever you want but do not return 0 (but don't break the comparator contract!)
                        return 0;
                    return compareKeys;
                return compareValues;


// And there's nothing more to do: the entries are sorted by value!
for ( Map.Entry<String,Integer> entry : set ) {
    System.out.println("Set entries: " + entry.getKey() + " -> " + entry.getValue());

// But if you add them to an hashmap
Map<String,Integer> myMap = new HashMap<String,Integer>();
// When iterating over the set the order is still good in the println...
for ( Map.Entry<String,Integer> entry : set ) {
    System.out.println("Added to result map entries: " + entry.getKey() + " " + entry.getValue());
    myMap.put(entry.getKey(), entry.getValue());

// But once they are in the hashmap, the order is not kept!
for ( Integer value : myMap.values() ) {
    System.out.println("Result map values: " + value);
// Also this way doesn't work:
// Logic because the entryset is a hashset for hashmaps and not a treeset
// (and even if it was a treeset, it would be on the keys only)
for ( Map.Entry<String,Integer> entry : myMap.entrySet() ) {
    System.out.println("Result map entries: " + entry.getKey() + " -> " + entry.getValue());

// If you want to iterate on a map ordered by value, you need to remember:
// 1) Maps are only sorted by keys, so you can't sort them directly by value
// 2) So you simply CAN'T return a map to a sortMapByValue function
// 3) You can't reverse the keys and the values because you have duplicate values
//    This also means you can't neither use Guava/Commons bidirectionnal treemaps or stuff like that

// So you can:
// 1) only sort the values which is easy, but you loose the key/value link (since you have duplicate values)
// 2) sort the map entries, but don't forget to handle the duplicate value case (like i did)
// 3) if you really need to return a map, use a LinkedHashMap which keep the insertion order

The exec:

The output:

Set entries: E -> -1
Set entries: B -> 1
Set entries: A -> 3
Set entries: C -> 3
Set entries: D -> 5
Set entries: H -> 15
Set entries: G -> 79
Set entries: F -> 1000
Added to result map entries: E -1
Added to result map entries: B 1
Added to result map entries: A 3
Added to result map entries: C 3
Added to result map entries: D 5
Added to result map entries: H 15
Added to result map entries: G 79
Added to result map entries: F 1000
Result map values: 5
Result map values: -1
Result map values: 1000
Result map values: 79
Result map values: 3
Result map values: 1
Result map values: 3
Result map values: 15
Result map entries: D -> 5
Result map entries: E -> -1
Result map entries: F -> 1000
Result map entries: G -> 79
Result map entries: A -> 3
Result map entries: B -> 1
Result map entries: C -> 3
Result map entries: H -> 15

Hope it will help some folks

Related Questions

Sponsored Content

42 Answered Questions

[SOLVED] How do I sort a dictionary by value?

22 Answered Questions

40 Answered Questions

[SOLVED] Sort array of objects by string property value

38 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

19 Answered Questions

78 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

18 Answered Questions

[SOLVED] How do you sort a dictionary by value?

21 Answered Questions

[SOLVED] Check if a given key already exists in a dictionary

  • 2009-10-21 19:05:09
  • Mohan Gulati
  • 2724442 View
  • 2583 Score
  • 21 Answer
  • Tags:   python dictionary

14 Answered Questions

[SOLVED] Add new keys to a dictionary?

  • 2009-06-21 22:07:39
  • lfaraone
  • 2815913 View
  • 2171 Score
  • 14 Answer
  • Tags:   python dictionary

35 Answered Questions

Sponsored Content