7

I have a list of objects that I want to sort using different properties.

@Override
public int compareTo(Object obj)
{
    Field tab = (Field) obj;

    int objx = Integer.parseInt(tab.getX());
    // int objy = Integer.parseInt(tab.getY());

    int classX = Integer.parseInt(this.X);

    if (classX == objx)
      return 0;
    else if (classX > objx)
      return 1;
    else
      return -1;
}

What I have so far:

Collections.sort(list1); // But using property1 to sort
Collections.sort(list1); // But using property2 to sort

So, in first case I'm able to sort using property1, but how to sort using property2? I'm trying to sort using different parameters, but compareTo() accepts only one.

spongebob
  • 8,370
  • 15
  • 50
  • 83
SOP
  • 773
  • 3
  • 9
  • 26

4 Answers4

6

You can create two different comparators one for each property and pass that to

Collections.sort(list1, comparator1);
Collections.sort(list1, comparator2);
Rahul Yadav
  • 1,503
  • 8
  • 11
  • As shown above I am overriding the method compaerto(Object ob) – SOP Aug 26 '15 at 10:32
  • 1
    You will need to write two separate Comparator and override each compareTo() method and then pass them along. Currently you have written just one. You will need to write another one for different property. – Rahul Yadav Aug 26 '15 at 10:35
6

If you want to use two different property you need to use two different comparators.

A comparator is a class that implements the interface Comparator

public class EmpSort {
    static final Comparator<Employee> comp1 = new Comparator<Employee>() {
        public int compare(Employee e1, Employee e2) {
            // use prperty1 to sort
            return e2.hireDate().compareTo(e1.hireDate());
        }
    };
}
public class EmpSort2 {
    static final Comparator<Employee> comp2 = new Comparator<Employee>() {
        public int compare(Employee e1, Employee e2) {
            // use prperty2 to sort
            return e2.hireDate().compareTo(e1.hireDate());
        }
    };
}

Collections.sort(list1, comp1);//but using prperty1 to sort
Collections.sort(list1, comp2);//but using prperty2 to sort

One related question Collections sort(List<T>,Comparator<? super T>) method example

Community
  • 1
  • 1
Rodolfo
  • 1,091
  • 3
  • 13
  • 35
6
    Collections.sort(list1, new Comparator<T>() {
        public int compare(T arg0, T arg1) {
            //your compare logic for prperty1
        }
    });
    Collections.sort(list1, new Comparator<T>() {
        public int compare(T arg0, T arg1) {
            //your compare logic for prperty2
        }
    });
SteveL
  • 3,331
  • 4
  • 32
  • 57
5

Java 8 is providing some convenient functions in the Comparator interface.

Collections.sort(list, Comparator.comparingInt(Field::getX));
Collections.sort(list, Comparator.comparingInt(Field::getY));

If you want to nest the comparators there are again nice functions.

Collections.sort(list, Comparator.comparingInt(Field::getX).thenComparingInt(Field::getY));
Flown
  • 11,480
  • 3
  • 45
  • 62