Sorting ArrayList of Arraylist in java

后端 未结 7 1059
忘掉有多难
忘掉有多难 2021-01-19 03:16

I have an ArrayList of ArrayList of String.

In Outer ArrayList on each index each Inner ArrayList has four items have four parameters.

  1. Contacts Id
7条回答
  •  执念已碎
    2021-01-19 03:57

    Use the following Comparator:

    class MyComparator implements Comparator> {
        private static int indexToCompare = 1;
        @Override
        public int compare(ArrayList o1, ArrayList o2) {
            return o1.get(indexToCompare).compareTo(o2.get(indexToCompare));
        }
    
    }
    

    Here indexToCompare is the index of the arraylist which corresponds to Contact Name. In your case "1"

提交回复
热议问题