Sorting ArrayList of Arraylist in java

后端 未结 7 983
忘掉有多难
忘掉有多难 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:36

    Assuming your Lists in your List has Strings in the order id, name, address and number (i.e. name is at index 1), you can use a Comparator, as follows:

    List> list;
    Collections.sort(list, new Comparator> () {
        @Override
        public int compare(List a, List b) {
            return a.get(1).compareTo(b.get(1));
        }
    });
    

    Incidentally, it matters not that you are using ArrayList: It is good programming practice to declare variables using the abstract type, i.e. List (as I have in this code).

提交回复
热议问题