Sorting ArrayList of Arraylist in java

后端 未结 7 981
忘掉有多难
忘掉有多难 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<String>> list;
    Collections.sort(list, new Comparator<List<String>> () {
        @Override
        public int compare(List<String> a, List<String> 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).

    0 讨论(0)
  • 2021-01-19 03:42
      import java.util.Collections;
      import java.util.Comparator;
      import java.util.List;
    
    
      public class ListsUtils {
    
          public static void sortListOfLists(List < List < String >> listOfLists) {
    
              // first sort the inner arrays using collections.sort
              for (List < String > innerList: listOfLists) {
                  Collections.sort(innerList);
              }
    
              // now sort by comparing the first string of each inner list using a comparator
              Collections.sort(listOfLists, new ListOfStringsComparator());
          }
    
          static final class ListOfStringsComparator implements Comparator < List < String >> {
    
              @
              Override
              public int compare(List < String > o1, List < String > o2) {
                  // do other error checks here as well... such as null. outofbounds, etc
                  return o1.get(0).compareTo(o2.get(0));
              }
    
          }
      }
    

    I guess I just assumed you had to sort a list of string arrays... thats why I sorted the list of inner arrays first, then sorted the outer list by comparing the 1st item of each array. Didnt read the contacts you had in your answer.

    In that case remove the for loop for sorting the inner list and you should still be able to sort using the comparator, but compare to the right index instead of the 1st element.

    Collections.sort(listOfLists, new ListOfStringListComparator());

    0 讨论(0)
  • 2021-01-19 03:45

    I think this is a case of not treating collections as first-class objects. Have a new class called "Contact" instead of abstracting it as an ArrayList, and use the Comparator.

    0 讨论(0)
  • 2021-01-19 03:48

    I feel bad posting this, because List<Contact> would be the much better choice. Something like this would be possible, though:

    ArrayList<ArrayList<String>> yourList = ...
    Collections.sort(yourList, new Comparator<ArrayList<String>>() {
        @Override
        public int compare(ArrayList<String> one, ArrayList<String> two) {
            return one.get(1).compareTo(two.get(1));
        }
    });
    
    0 讨论(0)
  • 2021-01-19 03:49

    Comparator / Comparable Interfaces can't help because I don't have any objects.

    Incorrect. You do have objects. All of the things you are trying to sort are objects.

    If you are trying to sort the ArrayList<String> objects in an ArrayList<ArrayList<String>>, you need to implement a Comparator<ArrayList<String>>. (The Comparable approach is the wrong one for this data structure. You would need to declare a custom subclass of ArrayList ... and that's yuck!)


    But a better idea would be to represent your objects with custom classes. In this case, your ArrayList of String should be a custom Contact class with 4 fields, getters and (if required) setters. Then you declare that as implementing Comparable<Contact>, and implement the compareTo method.


    Other Answers show how to implement a Comparator based on just one field of the list. That may be sufficient, but it will give you a sort order where the order of a pair of different "John Smith"s would be indeterminate. (I would use a second field as a tie-breaker. The Id field would be ideal if the ids are unique.)

    0 讨论(0)
  • 2021-01-19 03:57

    Use the following Comparator:

    class MyComparator implements Comparator<ArrayList<String>> {
        private static int indexToCompare = 1;
        @Override
        public int compare(ArrayList<String> o1, ArrayList<String> 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"

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