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);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2mecode
  • 21
  • 1
  • 3

3 Answers3

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 ());
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
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
thunderblaster
  • 918
  • 11
  • 27
Rick0Shea
  • 21
  • 1
1

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140