[java] How to use Comparator in Java to sort

Use People implements Comparable<People> instead; this defines the natural ordering for People.

A Comparator<People> can also be defined in addition, but People implements Comparator<People> is not the right way of doing things.

The two overloads for Collections.sort are different:

  • <T extends Comparable<? super T>> void sort(List<T> list)
    • Sorts Comparable objects using their natural ordering
  • <T> void sort(List<T> list, Comparator<? super T> c)
    • Sorts whatever using a compatible Comparator

You're confusing the two by trying to sort a Comparator (which is again why it doesn't make sense that Person implements Comparator<Person>). Again, to use Collections.sort, you need one of these to be true:

  • The type must be Comparable (use the 1-arg sort)
  • A Comparator for the type must be provided (use the 2-args sort)

Related questions


Also, do not use raw types in new code. Raw types are unsafe, and it's provided only for compatibility.

That is, instead of this:

ArrayList peps = new ArrayList(); // BAD!!! No generic safety!

you should've used the typesafe generic declaration like this:

List<People> peps = new ArrayList<People>(); // GOOD!!!

You will then find that your code doesn't even compile!! That would be a good thing, because there IS something wrong with the code (Person does not implements Comparable<Person>), but because you used raw type, the compiler didn't check for this, and instead you get a ClassCastException at run-time!!!

This should convince you to always use typesafe generic types in new code. Always.

See also

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to sorting

Sort Array of object by object field in Angular 6 Sorting a list with stream.sorted() in Java How to sort dates from Oldest to Newest in Excel? how to sort pandas dataframe from one column Reverse a comparator in Java 8 Find the unique values in a column and then sort them pandas groupby sort within groups pandas groupby sort descending order Efficiently sorting a numpy array in descending order? Swift: Sort array of objects alphabetically

Examples related to comparator

Reverse a comparator in Java 8 Java TreeMap Comparator Java get String CompareTo as a comparator object Java error: Comparison method violates its general contract Simple way to sort strings in the (case sensitive) alphabetical order java comparator, how to sort by integer? Android-java- How to sort a list of objects by a certain value within the object how to sort an ArrayList in ascending order using Collections and Comparator "Comparison method violates its general contract!" Java Comparator class to sort arrays