I have an arrayList of objects and I want to run a series of sort operations on this list. I would want to sort them by first by name, and if two names are same than 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 code sorting list by id
, but again i want to sort on name