Sort ArrayList from Arraylist <String> in java
I have an ArrayList from an ArrayList string.
In an Outer ArrayList for each index, each Inner ArrayList has four elements that have four parameters.
- Contact id
- Contact Name
- Contact Address
- Contact number
Now I want to sort the full ArrayList array based on the Contact Name parameter.
So, I want to access the external Arraylist, and the internal ArrayList present in each index of the external Arraylist must be sorted according to the name of the contact.
Comparator / comparable interfaces are unlikely to help me.
Collection.sort can't help me
Sort Arraylist from Arraylist Bean . I read this post, but this is for ArrayList of ArrayList<Object> . How to find out this problem?
Assuming your lists in your list have lines in identifier, name, address and order number (i.e. pointer name 1), you can use Comparator as shown below:
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)); } }); By the way, the point is not that you are using an ArrayList : it is good programming practice to declare variables using an abstract type, i.e. List (as I have in this code).
I feel bad because List<Contact> will be much better . Perhaps something like this is possible:
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)); } }); 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 assume that I just assumed that you need to sort the list of string arrays ... that is why I first sorted the list of internal arrays and then sorted the external list by comparing the 1st element of each array. I did not read the contacts that you had in your answer.
In this case, delete the for loop to sort the internal list, and you can still sort it using the comparator, but compare with the correct index instead of the 1st element.
Collections.sort (listOfLists, new ListOfStringListComparator ());
Comparator / comparable interfaces cannot help because I have no objects.
Wrong. You have objects. All the things you are trying to sort are objects.
If you are trying to sort ArrayList<String> objects into ArrayList<ArrayList<String>> , you need to implement Comparator<ArrayList<String>> . (A comparable approach is incorrect for this data structure. You will need to declare a custom subclass of ArrayList ... and this yuck!)
But it would be better to represent your objects with custom classes. In this case, your ArrayList of String should be a regular Contact class with 4 fields, getters and (if required) setters. Then you declare that you implement Comparable<Contact> and implement the compareTo method.
Other answers show how to implement a comparator based on only one list box. This may be enough, but it will give you a sort order where the order of a pair of different "John Smith" will be undefined. (I would use the second field as a tie-breaker. An Id field would be ideal if the identifiers are unique.)
You must create a class for your data structure (if you cannot, then indicate why. I see no good reason):
public class Contact implements Comparable { private String id; private String name; private String address; private String number; // Getters and setters, and compareTo. } Then use this on your list:
List<Contact> contacts = new ArrayList<Contacts>(); Sorting will be trivial.
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 array that corresponds to the contact name. In your case, "1"