System.out.println("bee".compareTo("kangaroo"));
System.out.println("zebra".compareTo("aardvark"));Output from running code snippet:
0
-9
25
It is the int compareTo(T o)
method of Comparable
that the Arrays.sort(Object[])
method uses to sort object arrays.
String
objects.String[] a = {"zebra", "bee", "aardvark", "kangaroo", "deer"};
System.out.println("before sort: "+java.util.Arrays.toString(a));
java.util.Arrays.sort(a);
System.out.println("after sort: "+java.util.Arrays.toString(a));
before sort: [zebra, bee, aardvark, kangaroo, deer]
after sort: [aardvark, bee, deer, kangaroo, zebra]
As Arrays.sort(Object[] a)
relies on int compareTo(T o)
to sort the array elements, an exception will be thrown if it is passed an array of objects that do not implment Comparable
.
Arrays.sort(Object[] a)
with instances of a class that does not implement Comparable
.Example.java
:import java.util.Arrays;
public class Example {
public static void main(String[] args) {
A[] a = new A[]{new A(2), new A(3), new A(5), new A(1), new A(4)};
System.out.println("before sort: "+Arrays.toString(a));
Arrays.sort(a);
System.out.println("after sort: "+Arrays.toString(a));
}
}
class A {
final int i;
A(int i) {
this.i = i;
}
@Override
public String toString() {
return "[A "+i+"]";
}
}
Example.java
:javac Example.java
Example
:java Example
Example
:before sort: [[A 2], [A 3], [A 5], [A 1], [A 4]]
Exception in thread "main" java.lang.ClassCastException: A cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at Example.main(Example.java:7)
If you want to use Arrays.sort(Object[] a)
with your own classes you can mark them as implementing Comparable
and define a int compareTo(T o)
that is relevant to the specific purpose of the class.
Arrays.sort(Object[] a)
with instances of a class that implements Comparable
.Example.java
:import java.util.Arrays;
public class Example {
public static void main(String[] args) {
A[] a = new A[]{new A(2), new A(3), new A(5), new A(1), new A(4)};
System.out.println("before sort: "+Arrays.toString(a));
Arrays.sort(a);
System.out.println("after sort: "+Arrays.toString(a));
}
}
class A implements Comparable<A> {
final int i;
A(int i) {
this.i = i;
}
@Override
public int compareTo(A o) {
if (i<o.i) {
return -1;
} else if (i>o.i) {
return 1;
} else {
return 0;
}
}
@Override
public String toString() {
return "[A "+i+"]";
}
}
Example.java
:javac Example.java
Example
:java Example
Example
:before sort: [[A 2], [A 3], [A 5], [A 1], [A 4]]
after sort: [[A 1], [A 2], [A 3], [A 4], [A 5]]
java.util.Comparator
interfaceThe Comparator
interface defines a int compare(T o1, T o2)
method. Implementations of int compare(T o1, T o2)
should return a negative integer if the first argument is less than the second; zero if the two arguments are equal; or a positive integer if the first argument is greater than the second.
By creating you own Comparator
it is possible to sort instances of a class that does not implement Comparable
.
Arrays.sort(Object[] a, Comparator)
to sort instances of a class that does not implement Comparable
.Example.java
:import java.util.Arrays;
import java.util.Comparator;
public class Example {
public static void main(String[] args) {
A[] a = new A[]{new A(2), new A(3), new A(5), new A(1), new A(4)};
System.out.println("before sort: "+Arrays.toString(a));
Arrays.sort(a, new AComparator());
System.out.println("after sort: "+Arrays.toString(a));
}
}
class A {
final int i;
A(int i) {
this.i = i;
}
@Override
public String toString() {
return "[A "+i+"]";
}
}
class AComparator implements Comparator<A> {
public int compare(A o1, A o2) {
if (o1.i<o2.i) {
return -1;
} else if (o1.i>o2.i) {
return 1;
} else {
return 0;
}
}
}
Example.java
:javac Example.java
Example
:java Example
Example
:before sort: [[A 2], [A 3], [A 5], [A 1], [A 4]]
after sort: [[A 1], [A 2], [A 3], [A 4], [A 5]]
Sometimes you may be required to sort instances of Comparable
using a differant set of criteria than the ones provided by their implementation of int compareTo(T o)
. By creating you own Comparator
you can define your own set of rules for deciding if objects are less than, equal or greater than other instances of the same class.
Comparator
to sort String
objects by length.Example.java
:import java.util.Arrays;
import java.util.Comparator;
public class Example {
public static void main(String[] args) {
String[] a = {"zebra", "bee", "aardvark", "kangaroo", "deer"};
System.out.println("before sort: "+Arrays.toString(a));
Arrays.sort(a, new StringLengthComparator());
System.out.println("after sort: "+Arrays.toString(a));
}
}
class StringLengthComparator implements Comparator<String> {
public int compare(String o1, String o2) {
if (o1.length()<o2.length()) {
return -1;
} else if (o1.length()>o2.length()) {
return 1;
} else {
return o1.compareTo(o2);
}
}
}
Example.java
:javac Example.java
Example
:java Example
Example
:before sort: [zebra, bee, aardvark, kangaroo, deer]
after sort: [bee, deer, zebra, aardvark, kangaroo]