How do I sort an array?

John Zukowski

The Arrays class in java.util provides a series of sort() methods for sorting arrays. If the array is an array of primitives or an array of a class that implements Comparable then you can just call the method directly:


If, however, it is an array of objects that don't implement the Comparable interface then you need to provide a custom Comparator to help you sort the elements in the array.

Arrays.sort(theArray, theComparator);