sort an arraylist of arraylist of integers

后端 未结 3 1196
庸人自扰
庸人自扰 2021-01-06 04:57

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 coll

相关标签:
3条回答
  • 2021-01-06 05:27

    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);
    
    0 讨论(0)
  • 2021-01-06 05:30

    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());
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-06 05:35

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

    0 讨论(0)
提交回复
热议问题