1

I want to sort a HashMap by values. I'm currently using the following code

public static Map<String, Float> sortByValue(Map<String, Float> unsorted_map){

    Map<String, Float> sorted_map = new LinkedHashMap<String, Float>();

    try{
        // 1. Convert Map to List of Map
        List<Map.Entry<String, Float>> list = new LinkedList<Map.Entry<String, Float>>(unsorted_map.entrySet());

        // 2. Sort list with Collections.sort(), provide a custom Comparator
        Collections.sort(list, new Comparator<Map.Entry<String, Float>>() {
            public int compare(Map.Entry<String, Float> o1,
                    Map.Entry<String, Float> o2) {
                return (o1.getValue()).compareTo(o2.getValue());
            }
        });

        // 3. Loop the sorted list and put it into a new insertion order Map LinkedHashMap
        for (Map.Entry<String, Float> entry : list) {
            sorted_map.put(entry.getKey(), entry.getValue());
        }

    }
    catch(Exception e){
        e.printStackTrace();
    } 

    return sorted_map;

}

but the output is wrong. In particular, values begin in descending order then, after a 0.0 value, they become in ascending order (as the second data column shows).

---> 1985 0.54660827
---> 2002 0.45290402
---> 1993 0.40605184
---> 2015 0.3591997
---> 2000 0.34358233
---> 1980 0.2811128
---> 2008 0.24987808
---> 1990 0.23426071
---> 1983 0.23426071
---> 1986 0.21864335
---> 1977 0.18740855
---> 1992 0.1717912
---> 2009 0.14055647
---> 2006 0.12493904
---> 2011 0.031234872
---> 2001 0.015617436
---> 1975 0.0
---> 1997 0.031234723
---> 2004 0.046852008
---> 1988 0.062469445
---> 2013 0.07808688
---> 1978 0.07808688
---> 2005 0.1093216
---> 1981 0.14055632
---> 1976 0.14055632
---> 2014 0.14055632
---> 1999 0.15617377
---> 1994 0.20302577
---> 1987 0.20302577
---> 2010 0.2186432
---> 1991 0.24987794
---> 1989 0.24987794
---> 1996 0.28111264
---> 2012 0.28111264
---> 2003 0.29673007
---> 1998 0.29673007
---> 2007 0.31234753
---> 1984 0.3904344
---> 1982 0.42166913
---> 1979 0.5309907
---> 1995 0.73401666

Someone can help me?

Thanks in advance!

8
  • Why not use a TreeMap which sorts itself? Commented Sep 16, 2016 at 13:43
  • @puhlen because it sorts by key, not by value. Commented Sep 16, 2016 at 13:44
  • This implementation looks correct to me. I'd honestly actually suspect the code you're using to print out the map, or not assigning the sorted map somehow when you call this method. Commented Sep 16, 2016 at 13:45
  • This code works fine in other point of the application. Commented Sep 16, 2016 at 13:48
  • So I tried to replicate your problem and I wasn't able to repl.it/DbOU/0. Can you show a sample of data that will show off your problem? Commented Sep 16, 2016 at 13:50

4 Answers 4

3

Sorting code is OK. Your print seems to fail. Check negative numbers printing.

Try this sorting using Stream API

LinkedHashMap<String, Integer> map 
    = unsorted_map.entrySet()
            .stream()
            .sorted(Map.Entry.comparingByValue(Integer::compareTo))
            .collect(Collectors.toMap(
                Map.Entry::getKey,
                Map.Entry::getValue,
                (a, b) -> a,
               LinkedHashMap::new
            ));

Collector make LinkedHashMap node by identity with "take one" collision resolver.

Sign up to request clarification or add additional context in comments.

Comments

1

I think your code some should works. i write code like you and it works, here is my code

public static void main(String[] args) {
    Map<Integer, Float> unsortFloatMap = new HashMap<Integer, Float>();
    unsortFloatMap.put(1985, 0.54660827f);
    unsortFloatMap.put(2002, 0.45290402f);
    unsortFloatMap.put(1993, 0.40605184f);
    unsortFloatMap.put(2015, 0.3591997f);

    unsortFloatMap.put(1975, 0.0f);
    unsortFloatMap.put(1997, 0.031234723f);
    unsortFloatMap.put(2004, 0.046852008f);
    unsortFloatMap.put(2005, 0.1093216f);
    unsortFloatMap.put(2003, 0.29673007f);
    unsortFloatMap.put(1998, 0.29673007f);
    unsortFloatMap.put(2007, 0.31234753f);
    unsortFloatMap.put(1995, 0.73401666f);

    System.out.println("Unsort Float Map......");
    printMap(unsortFloatMap);

    System.out.println("Sorted Float Map......");
    Map<Integer, Float> sortedFloatMap = sortByFloatValue(unsortFloatMap);
    printMap(sortedFloatMap);
}

// pretty print a map
public static <K, V> void printMap(Map<K, V> map) {
    for (Map.Entry<K, V> entry : map.entrySet()) {
        System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
    }
}

private static Map<Integer, Float> sortByFloatValue(Map<Integer, Float> unsortMap) {
    List<Map.Entry<Integer, Float>> list = new LinkedList<Map.Entry<Integer, Float>>(unsortMap.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<Integer, Float>>() {
        public int compare(Map.Entry<Integer, Float> o1, Map.Entry<Integer, Float> o2) {
            return (o1.getValue()).compareTo(o2.getValue());
        }
    });

    /// Loop the sorted list and put it into a new insertion order Map
    /// LinkedHashMap
    Map<Integer, Float> sortedMap = new LinkedHashMap<Integer, Float>();
    for (Map.Entry<Integer, Float> entry : list) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }

    return sortedMap;
}

Output is:

Unsort Float Map......
Key : 2002 Value : 0.45290402
Key : 2003 Value : 0.29673007
Key : 1985 Value : 0.54660827
Key : 1975 Value : 0.0
Key : 2007 Value : 0.31234753
Key : 2004 Value : 0.046852008
Key : 2005 Value : 0.1093216
Key : 1995 Value : 0.73401666
Key : 1993 Value : 0.40605184
Key : 1998 Value : 0.29673007
Key : 2015 Value : 0.3591997
Key : 1997 Value : 0.031234723
Sorted Float Map......
Key : 1975 Value : 0.0
Key : 1997 Value : 0.031234723
Key : 2004 Value : 0.046852008
Key : 2005 Value : 0.1093216
Key : 2003 Value : 0.29673007
Key : 1998 Value : 0.29673007
Key : 2007 Value : 0.31234753
Key : 2015 Value : 0.3591997
Key : 1993 Value : 0.40605184

Comments

1

The most reasonable explanation is that your printing code fails to print the - in front of negative numbers. Your sorting code is correct.

Comments

0
package com.JavaExample;
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.HashSet; 
import java.util.List; 
import java.util.TreeSet;

public class HashSetSorted 
{

    public static void main(String[] args)
    {
        HashSet<String> names = new HashSet<String>(); 
        names.add("Asker");
        names.add("Crak"); 
        names.add("Bayliss"); 
        names.add("Mohna"); 
        names.add("Dina");
        System.out.println("HashSet before sorting : " + names); 
        // Sorting HashSet using List 
        List<String> tempList = new ArrayList<String>(names);
        Collections.sort(tempList); 
        System.out.println("HashSet element in sorted order : " + tempList);     
        // Sorting HashSet using TreeSet
        TreeSet<String> sorted = new TreeSet<String>(names);
        System.out.println("HashSet sorted using TreeSet : " + sorted);
    }

}

1 Comment

Please fix the code formatting. Indent 4 spaces on each line to make a code block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.