0

I'm trying to sort my HasMap ArrayList so my listview is sorted by values but I'm not getting it. Basically I have several keys and one of them is "type" which holds values like "1", "4", "3",....

I want to order the list by this key "type" but I'm getting "1", "11", "2" instead of "1", "2", "11"...

I'm trying this code to sort it:

Collections.sort(myList, new Comparator<HashMap<String, String>>() {
public int compare(HashMap<String, 
String> mapping1,HashMap<String, String> mapping2) {
return mapping1.get("type").compareTo(mapping2.get("type"));
    }
});
3
  • Why are you using a String for the key, when you're using it purely to store ints? Commented Apr 15, 2013 at 7:47
  • There's more keys in there that aren't ints. When I try to change that key to int I get an error on my map.put(key,value) Commented Apr 15, 2013 at 8:09
  • In that case, beware of NumberFormatException that will get thrown by your accepted answer - you'll need to expand this logic to accommodate your valid String keys. docs.oracle.com/javase/1.5.0/docs/api/java/lang/… Commented Apr 15, 2013 at 9:44

4 Answers 4

5

Your type is a String and thats why you are getting "1", "11", "2". Convert that string to integer (Integer.valueOf()) and then compare.

change the following

mapping1.get("type").compareTo(mapping2.get("type"));

to

 Integer.valueOf(mapping1.get("type")).compareTo(Integer.valueOf(mapping2.get("type")));

Note: I did not compile the above code.

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

Comments

1

The data-type of "type" seems to be String. Hence the sorting "1", "11", "2" seems correct. Change the data-type of "type" to Integer

OR

in compare method compare Integer.parseInt values of the "type"

Comments

0

You'll need to handle non-integer values in your comparator, if, as you mentioned above, you expect to have a mixture of String and Integer keys.

Collections.sort(myList, new Comparator<HashMap<String, String>>() {
    public int compare(HashMap<String, String> mapping1,
                       HashMap<String, String> mapping2) {
        String valueOne = mapping1.get("type");
        String valueTwo = mapping2.get("type");
        try {
            return Integer.valueOf(valueOne).compareTo(Integer.valueOf(valueTwo));
        } catch(NumberFormatException e) {
            return valueOne.compareTo(valueTwo);
        }
    }
});

(Otherwise, the key value should be changed to Integer to avoid mistakes by other developers.)

Comments

0

You can do as below..

Change parameter as per your need..

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 (o2.getValue()).compareTo( o1.getValue() );
            }
        } );
        for(Map.Entry<String, Integer> entry:list){
            System.out.println(entry.getKey()+" ==== "+entry.getValue());
        }

Comments

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.