简体   繁体   中英

sort an arraylist of arraylist of integers

I am looking to sort an arraylist of arraylist of integers and I require help?

I was informed that I need to implement comparator or comparable and then use the collection.sort to sort the list of list in order...

ArrayList<ArrayList<Integer>> g = new ArrayList<ArrayList<Integer>>()

If you look at the list of list as the following example:
C1 – 5,4,10
C2 – 3,2,1
C3 – 7,8,6
First it will be sorted like this:
C1 – 4,5,10
C2 – 1,2,3
C3 – 6,7,8
Then it will be sorted like this
C1 – 1,2,3
C2 – 4,5,6
C3 – 7,8,10

No error check for null lists, but here it is.

List<List<Integer>> list = Arrays.asList(Arrays.asList(10, 5, 4), 
        Arrays.asList(3, 2, 1), Arrays.asList(7, 8, 6));
for (List<Integer> l : list) {
    Collections.sort(l);
}
Collections.sort(list, new Comparator<List<Integer>>() {
    public int compare(List<Integer> o1, List<Integer> o2) {
        return o1.get(0).compareTo(o2.get(0));
    }
});
System.out.println(list);

With Java 8 it gets even more concise:

List<List<Integer>> list = Arrays.asList(Arrays.asList(10, 5, 4),
                Arrays.asList(3, 2, 1), Arrays.asList(7, 8, 6));
list.forEach(Collections::sort);
Collections.sort(list, (l1, l2) -> l1.get(0).compareTo(l2.get(0)));
System.out.println(list);

You could just sort each list individually. The Collections.sort(collection) will sort the Integers in ascending order automatically.

if sort doesnt have what u need you can try this algorithm:

package drawFramePackage;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Random;
public class QuicksortAlgorithm {
    ArrayList<AffineTransform> affs;
    ListIterator<AffineTransform> li;
    Integer count, count2;
    /**
     * @param args
     */
    public static void main(String[] args) {
        new QuicksortAlgorithm();
    }
    public QuicksortAlgorithm(){
        count = new Integer(0);
        count2 = new Integer(1);
        affs = new ArrayList<AffineTransform>();
        for (int i = 0; i <= 128; i++){
            affs.add(new AffineTransform(1, 0, 0, 1, new Random().nextInt(1024), 0));
        }
        affs = arrangeNumbers(affs);
        printNumbers();
    }
    public ArrayList<AffineTransform> arrangeNumbers(ArrayList<AffineTransform> list){
        while (list.size() > 1 && count != list.size() - 1){
            if (list.get(count2).getTranslateX() > list.get(count).getTranslateX()){
                list.add(count, list.get(count2));
                list.remove(count2 + 1);
            }
            if (count2 == list.size() - 1){
                count++;
                count2 = count + 1;
            }
            else{
            count2++;
            }
        }
        return list;
    }
    public void printNumbers(){
        li = affs.listIterator();
        while (li.hasNext()){
            System.out.println(li.next());
        }
    }
}

This works for me

Collections.sort(list, new Comparator<List<Integer>>() { 
            public int compare(List<Integer> o1, List<Integer> o2) { 
                int min = Math.min(o1.size(),o2.size());
                for(int i=0;i<min;i++)
                {
                     if(o1.get(i)!=o2.get(i))
                     {
                         return o1.get(i).compareTo(o2.get(i));
                     }
                }
                return (o1.size()<=o2.size())? -1:1;                
            }
        });

It will check up to last element of the list and sort them according to the requirement.

Collections.sort(res, new MIN());

//method

static class MIN implements Comparator<ArrayList<Integer>>
    {
        public int compare(ArrayList<Integer> a,ArrayList<Integer> b)
        {
            for(int i=0;i<Math.min(a.size(),b.size());i++)
            {
                if(a.get(i)>b.get(i)) return 1;
                else if(a.get(i)<b.get(i)) return -1;
                else continue;
            }
            return a.size()-b.size();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM