How to perform operation on Arraylist?

How to perform a series of sort operation on arraylist

  • 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

  • Answer:

    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 the comparison you want 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()); } }

Romi at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

You can write a Comparator from a collection of comparators. 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));

Peter Lawrey

As you said, and similar as the code you already wrote you would create a class that implements the Comparator interface for Ticket. First, you compare the ticket's names using a String comparator, and if that results in 0 (equal name) then you would compare by id in the same comparator. Make sure the string comparator does trimming before (remove before and after empty spaces) and maybe ignore casing, that's up to you. If you want some kind of generalization, then you can write a decorator comparator which calls the more specifics ones. If you want to know more about this, let me know.

Luciano

You need to use a stable sorting algorithm, then sort by ID first and after that sort by name. Check out http://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms to find one that suits your needs.

okrumnow

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 with 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 give list with sorted by name

varsha

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.