You can make objects comparable by implementing the java.lang.Comparable and java.util.Comparator interfaces.
Classes such as java.lang.String,
java.util.Date, and primitive wrapper classes all implement java.lang.Comparable.
import java.util.Arrays;
public class MainClass {
public static void main(String[] args) {
int[] intArray = new int[] { 5, 4, 3, 2, 1 };
Arrays.sort(intArray);
for (int i : intArray) {
System.out.println(i);
}
}
}
1
2
3
4
5