Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collections sort(List<T>,Comparator<? super T>) method example [duplicate]

Tags:

java

Possible Duplicate:
Sorting Java objects using multiple keys

I can't find any example of using this method, all examples give the second parameter "null". I heard that this method used for sorting classes according to more than one criterion but no example where found.

public class Student implements Comparable<Student> { String name; int age;  public Student(String name, int age) {     this.name = name;     this.age = age; }  @Override public String toString() {     return name + ":" + age; }  @Override public int compareTo(Student o) {     Integer myAge = age;     Integer oAge = o.age;     return myAge.compareTo(oAge); } 

}

for this class if i want to sort a list of Student according to their names & ages how can i use the method Collections sort(List,Comparator)

like image 266
Tarek Avatar asked Jan 04 '13 09:01

Tarek


People also ask

How do I use Comparator with collections sort?

Programmers frequently need to sort elements from a database into a collection, array, or map. In Java, we can implement whatever sorting algorithm we want with any type. Using the Comparable interface and compareTo() method, we can sort using alphabetical order, String length, reverse alphabetical order, or numbers.

How do you write a Comparator method in Java?

Method 2: Using comparator interface- Comparator interface is used to order the objects of a user-defined class. This interface is present in java. util package and contains 2 methods compare(Object obj1, Object obj2) and equals(Object element). Using a comparator, we can sort the elements based on data members.

Does collections sort use compareTo?

Collections class has a second sort() method and it takes Comparator. The sort() method invokes the compare() to sort objects.


1 Answers

Building upon your existing Student class, this is how I usually do it, especially if I need more than one comparator.

public class Student implements Comparable<Student> {      String name;     int age;      public Student(String name, int age) {        this.name = name;        this.age = age;     }      @Override     public String toString() {         return name + ":" + age;     }      @Override     public int compareTo(Student o) {         return Comparators.NAME.compare(this, o);     }       public static class Comparators {          public static Comparator<Student> NAME = new Comparator<Student>() {             @Override             public int compare(Student o1, Student o2) {                 return o1.name.compareTo(o2.name);             }         };         public static Comparator<Student> AGE = new Comparator<Student>() {             @Override             public int compare(Student o1, Student o2) {                 return o1.age - o2.age;             }         };         public static Comparator<Student> NAMEANDAGE = new Comparator<Student>() {             @Override             public int compare(Student o1, Student o2) {                 int i = o1.name.compareTo(o2.name);                 if (i == 0) {                     i = o1.age - o2.age;                 }                 return i;             }         };     } } 

Usage:

List<Student> studentList = new LinkedList<>(); Collections.sort(studentList, Student.Comparators.AGE); 

EDIT

Since the release of Java 8 the inner class Comparators may be greatly simplified using lambdas. Java 8 also introduces a new method for the Comparator object thenComparing, which removes the need for doing manual checking of each comparator when nesting them. Below is the Java 8 implementation of the Student.Comparators class with these changes taken into account.

public static class Comparators {     public static final Comparator<Student> NAME = (Student o1, Student o2) -> o1.name.compareTo(o2.name);     public static final Comparator<Student> AGE = (Student o1, Student o2) -> Integer.compare(o1.age, o2.age);     public static final Comparator<Student> NAMEANDAGE = (Student o1, Student o2) -> NAME.thenComparing(AGE).compare(o1, o2); } 
like image 111
atomman Avatar answered Oct 18 '22 09:10

atomman