2

How to sort array?

Before

{"A"=1, "B"=1, "C"=3}, 
{"A"=1, "B"=1, "C"=2},
{"A"=1, "B"=11, "C"=2},
{"A"=1, "B"=2, "C"=2},
{"A"=1, "B"=1, "C"=11}

After

{"A"=1, "B"=1, "C"=2},
{"A"=1, "B"=1, "C"=3},
{"A"=1, "B"=1, "C"=11},
{"A"=1, "B"=2, "C"=2}, 
{"A"=1, "B"=11, "C"=2}

Code

    ArrayList mylist = new ArrayList();

    Map<String,Integer> mMap = new HashMap<String,Integer>();
    mMap.put("A",1);
    mMap.put("B",1);
    mMap.put("C",3);
    mylist.add(mMap); 

    mMap = new HashMap<String,Integer>();
    mMap.put("A",1);
    mMap.put("B",1);
    mMap.put("C",2);
    mylist.add(mMap); 

    mMap = new HashMap<String,Integer>();
    mMap.put("A",1);
    mMap.put("B",11);
    mMap.put("C",2);
    mylist.add(mMap);

    mMap = new HashMap<String,Integer>();
    mMap.put("A",1);
    mMap.put("B",2);
    mMap.put("C",2);
    mylist.add(mMap);

    mMap = new HashMap<String,Integer>();
    mMap.put("A",1);
    mMap.put("B",1);
    mMap.put("C",11);       
    mylist.add(mMap);
5
  • 1
    What have you tried? Commented Feb 4, 2013 at 7:23
  • 1
    You have the same question with this: stackoverflow.com/questions/780541/how-to-sort-hash-map Commented Feb 4, 2013 at 7:26
  • Note that since a map has no inherent order, you will need to specify the order of key sorting explicitly. Commented Feb 4, 2013 at 7:26
  • Why don't you use Java objects (with attributes a, b and c) rather than using maps? Would be much clearer and safer, allow encapsulation, and be more efficient. Commented Feb 4, 2013 at 7:33
  • @AndrewThompson LinkedHashMap a better choice here to maintain insertion order? . Agree with JBNizet on using a POJO Commented Feb 4, 2013 at 7:54

3 Answers 3

9

Define your own comparator like this:

public class MyMapComparator implements Comparator<Map <String, Integer>> 
{
    @Override
    public int compare (Map<String, Integer> o1, Map<String, Integer> o2) 
    {
        int c;

        c = o1.get ("A").compareTo(o2.get ("A"));
        if (c != 0) return c;

        c = o1.get ("B").compareTo(o2.get ("B"));
        if (c != 0) return c;

        return o1.get ("C").compareTo(o2.get ("C"));
    }
}

Then use it to sort your list:

Collections.sort (mylist, new MyComparator ());
Sign up to request clarification or add additional context in comments.

Comments

2

Here is an example of how to sort an ArrayList of HashMaps that compiles and runs - I could not find such an example - so, unless I botched the cut & paste from my Dell mini which is easy to do, this should do it.

import java.util.*;
import java.lang.*;

public class test {

public static class mySort implements Comparator<HashMap<String,Integer>> {

    @Override
    public int compare(HashMap<String, Integer> map1, HashMap<String, Integer> map2) {
        return map1.get("number").intValue() - map2.get("number").intValue();
    }
}


static void displayAL(ArrayList<HashMap> al) {

    Iterator it = al.iterator();
    HashMap<String,Integer> hm;

    while (it.hasNext()) {

      hm = (HashMap<String,Integer>)it.next();
      System.out.println(hm.get("number"));

    }

    System.out.println();

}


public static void main(String[] args) {

    ArrayList<HashMap> al = new ArrayList<HashMap>();
HashMap<String,Integer> hm = new HashMap<String,Integer>();

hm.put("number",10);
al.add(hm);
hm = new HashMap<String,Integer>();
hm.put("number",2);
al.add(hm);
hm = new HashMap<String,Integer>();
hm.put("number",0);
al.add(hm);
hm = new HashMap<String,Integer>();
hm.put("number",-3);
al.add(hm);

displayAL(al);

Collections.sort((ArrayList)al,new mySort());

System.out.println("sorted:\n");

displayAL(al);

  }
}


rick@rick-jolicloud:~/javaProjects$ java test
10
2
0
-3

sorted:

-3
0
2
10

Comments

1

You need to use Collections.sort(list, comparator) where Comparator<Map> implements your custom comparison logic

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.