Java provides two methods for sorting elements in a collection or array, one is to implement the Comparable
interface, and the other is to implement the Comparator
interface.
Interface | package | method |
---|---|---|
Comparable<T> | java.lang | public int compareTo(T o); |
Comparator<T> | java.util | int compare(T o1, T o2); |
Comparable Interface
For example, Item implements Comparable
and compare their implementation class’s price.
The compareTo
method used to compare this
object with specific incoming object o
.
1 | public class Item implements Comparable<Item> { |
compareTo
method’s detail info
- param: o the object to be compared.
- return: a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
- throws: NullPointerException if the specified object is null
- throws: ClassCastException if the specified object’s type prevents it from being compared to this object.
It is strongly recommended, but not strictly required that
(x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any
class that implements the Comparable interface and violates
this condition should clearly indicate this fact. The recommended language is “Note: this class has a natural ordering that is inconsistent with equals.”
When you want to sort a list of Item
, you need just one line. Because Collections
will automatedly sort as the rule of the compareTo
method.
1 | Collections.sort(items); |
Extra Tips
If your comparing property is not an object, just a basic data type, we can not use thecompareTo
method to compare two data. Using mathematics symbol -
will work.
Comparator Interface
For example, customize a class to implements a Comparator
and put this comparator in a sort method. Provided 2 methods to implement Comparator
. One is to define a new class, the other is to create an anonymous inner class
.
There is only one method that must have to implement - compare
. This method is used to compare to Object o1
, o2
.
1 | public class Item { |
the method compare
‘s detail info
- param: o1 the first object to be compared.
- param: o2 the second object to be compared.
- return: a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
- throws: NullPointerException if an argument is null and this comparator does not permit null arguments.
- throws: ClassCastException if the arguments’ types prevent them from being compared by this comparator.
Arrays & Collections sort
Method | Comparisons |
---|---|
Arrays.sort(int[].., fromIndex, endIndex) | int[], double[], byte[]… |
Arrays.sort(T[], Comparator<? super T> c) | Integer[], Double[], Byte[]… |
Collections.sort(List<T>, Comparator<? super T> c) | List<Integer>, List<Double>, List<Byte>… |
Sort method’s rule
o1 is the left number, o2 is the next right number
- when comparator return <= 0, o1, o2 object don’t exchange
- when comparator return > 1, o1, o2 object exchange
Basic Data Type Code Example
Java API -Arrays.sort()
provide sort mthod for basic data type such as int[], double[], byte[]...
in an increasing order
and this is the only one sort methode provide for basic data type .If you want to change the original order, you should use wrapper class.
1 | public static void main(String[] args) { |
Wrapper Class Code Example
We can customize a comparator class to implement Comparator Interface
and put it in sort
method, but we have a more concise method to achieve it by using Lambda Expressions
or Comparator.comparingDouble()
, Comparator.comparing()
and so on.
The following code example shows wrapper data type
‘s natural and reverse sort method, you just need to pick your favorite one.
1 | public static void main(String[] args) { |
Object Code Example
The following code example shows complex data type
‘s natural and reverse sort methods, you just need to pick you favorite one.
1 | public static void main(String[] args) { |
Author’s Message
Recently I practiced algorithms on LeetCode. Under some conditions, I have to sort Array or List. I was confused by the different kinds of comparison methods. So I sorted out the above knowledge points.
Reference Links
https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html