How to perform a series of arraylist sort operations (multiple sort criteria)

I have an array of List objects, and I want to run a series of sorting operations on this list. I would like to sort them by name by name, and if the two names are the same, then sort them by id, for example.

How can I implement it?

This is my code.

Comparator<Ticket> mc; mc = new TicketIdComparator(); Collections.sort(tickets, mc); final class TicketIdComparator implements Comparator<Ticket> { @Override public int compare(Ticket ticket1, Ticket ticket2) { String TicketId1 = ((Ticket) ticket1).getNumber(); String TickedId2 = ((Ticket) ticket2).getNumber(); int num1=Integer.parseInt(TicketId1); int num2 =Integer.parseInt(TickedId2); if (num1<num2) return 1; if (num1>num2) return -1; return 0; } } 

This list is sorting code by id , but again I want to sort by name

+3
java collections sorting arraylist comparator
source share
6 answers
  Collections.sort(myList, new Comparator() { @Override public int compare(Object o1, Object o2) { // write your ordering code here return 0; } }); 

Just fill in the code for comparison, and Java will handle the sorting for you.

Edit for updated question:

 Comparator<Ticket> mc; mc = new TicketIdComparator(); Collections.sort(tickets, mc); final class TicketIdComparator implements Comparator<Ticket> { @Override public int compare(Ticket ticket1, Ticket ticket2) { String TicketId1 = ((Ticket) ticket1).getNumber(); String TickedId2 = ((Ticket) ticket2).getNumber(); int num1=Integer.parseInt(TicketId1); int num2 =Integer.parseInt(TickedId2); if (num1<num2) return 1; else if (num1>num2) return -1; else return ticket1.getName().compare(ticket2.getName()); } } 
+3
source share

You can write a Comparator from the comparator collection.

 public static <T extends Comparable<T>> Comparator<T> comparatorFrom(final Comparator<T>... comparators) { return new Comparator<T>() { @Override public int compare(T o1, T o2) { for (Comparator<T> comparator : comparators) { final int cmp = comparator.compare(o1, o2); if (cmp != 0) return cmp; } return 0; } }; } // you can then use Arrays.sort(list, comparatorFrom(comp1, comp2, comp3, comp4)); 
+5
source share

As you said, and in the same way as you wrote the code, you will create a class that implements the Comparator interface for Ticket. First, you compare ticket names using String Comparator, and if this results in 0 (equal name), then you compare by id in the same comparator.

Make sure that the string comparator performs trimming before (delete before and after empty spaces) and, possibly, ignore the casing, which is up to you.

If you want some kind of generalization, you can write a comparator-decorator that names more specific ones. If you want to know more about this, let me know.

+2
source share

Collections.sort (myList, new Comparator () {

  @Override public int compare(Object o1, Object o2) { // write your ordering code here for sorting list by id return 0; } 

});

list sorted by id

Collections.sort (myList, new Comparator () {

  @Override public int compare(Object o1, Object o2) { // write your ordering code here for sorting list by name return 0; } 

});

it will provide a list sorted by name

+1
source share

With Java 8, you can use lambdas and based on Peter Lowry's answer you can do something beautiful:

  Collections.sort(users, comparatorFrom( (final User u1, final User u2) -> u1.getDomain().getFirstName() .compareTo(u2.getFirstName()), (final User u1, final User u2) -> u1.getLastName().compareTo(u2.getLastName()), ...more comparators here... )); 
+1
source share

You need to use a stable sorting algorithm, and then sort by identifier first and then sort by name.

Check this comparison of algorithms to find one that suits your needs.

0
source share

All Articles