Archive for Collections

Making Your Objects Comparable and Sortable

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.

int[] intArray = new int[] { 5, 4, 3, 2, 1 };
Arrays.sort(intArray);
for (int i : intArray) {
System.out.println(i);

}

Leave a Comment