7.27.2014

Arrays: sort() – Sorts an array

In order to sort an array, java.util.Arrays class provides 18 overloaded sort() method for not only primitive types(excluding boolean) and Object, but also your custom sort implementation for your own objects.

For single argument (primitive types) sort() method, it sorts the specified array in ascending numerical order.

For three argument (primitive types) sort() method, the first argument is the array to be sorted, the second argument is the starting index fromIndex, inclusive, and the third index is the last index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.

By passing Object array to the sort() method, it sorts the specified array in ascending numerical order, according to the natural ordering of its elements. All elements must implement the Comparable interface. Furthermore, all elements in this range must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).

Exception:
IllegalArgumentException - if fromIndex > toIndex
IllegalArgumentException - (optional) if the natural ordering of the array elements is found to violate the Comparable contract
ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > array.length
ClassCastException - if the array contains elements that are not mutually comparable (for example, strings and integers)

Method Declaration:
public static void sort(byte[] a)
public static void sort(short[] a)
public static void sort(int[] a)
public static void sort(long[] a)
public static void sort(float[] a)
public static void sort(double[] a)
public static void sort(char[] a)
public static void sort(Object[] a)

public static void sort(byte[] a,  int fromIndex, int toIndex)

public static void sort(short[] a, int fromIndex, int toIndex)
public static void sort(int[] a, int fromIndex, int toIndex)
public static void sort(long[] a, int fromIndex, int toIndex)
public static void sort(float[] a, int fromIndex, int toIndex)
public static void sort(double[] a, int fromIndex, int toIndex)
public static void sort(char[] a, int fromIndex, int toIndex)
public static void sort(Object[] a, int fromIndex, int toIndex)

public static <T> void sort(T[] a, Comparator<? super T> c)

public static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)

Live Example:
import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {
        byte[] byteArray = {7, 0, 2, 6, 1 , 5, 1};
        short[] shortArray = {10, 7, 12, 0, 4, 15, 20};
        int[] intArray = {30, 25, 16 , 26, 40, 35};
        long[] longArray = {111L, 256L, 55L, 280L, 180L};
        float[] floatArray = {3.5F, 0.1F, 6.2F, 2.8F, 4.3F};
        double[] doubleArray = {10.15, 12.40, 7.5, 11.50, 8.9, 6.3};
        char[] charArray = {'x', 'c', 'd', 'a', 'm', 'k', 'j'};
        String[] stringArray = {"egg", "doll", "bat", "cat", "flower", "apple"};
        
      
        System.out.println("byteArrary: " + Arrays.toString(byteArray));
        Arrays.sort(byteArray);
        System.out.println("Arrays.sort(byteArray): " + Arrays.toString(byteArray) + "\n");
        System.out.println("shortArrary: " + Arrays.toString(shortArray));
        Arrays.sort(shortArray, 0, shortArray.length);
        System.out.println("Arrays.sort(shortArrary, 0, 4): " + Arrays.toString(shortArray) + "\n");
        System.out.println("intArrary: " + Arrays.toString(intArray));
        Arrays.sort(intArray, 2, 6);
        System.out.println("Arrays.sort(intArray, 2, 6): " + Arrays.toString(intArray) + "\n");
        System.out.println("longArrary: " + Arrays.toString(longArray));
        Arrays.sort(longArray, 0, 0);
        System.out.println("Arrays.sort(longArray, 0, 0): " + Arrays.toString(longArray) + "\n");
        System.out.println("floatArrary: " + Arrays.toString(floatArray));
        Arrays.sort(floatArray, 4, 4);
        System.out.println("Arrays.sort(floatArray, 4, 4): " + Arrays.toString(floatArray) + "\n");
        try {
            System.out.println("doubleArrary: " + Arrays.toString(doubleArray));
            Arrays.sort(doubleArray, 4, 1);
        } catch(IllegalArgumentException illArgExp) {
            System.out.println("Arrays.sort(doubleArray, 4, 1): " + illArgExp.getMessage() + "\n");
        }
        try {
            System.out.println("charArrary: " + Arrays.toString(charArray));
            Arrays.sort(charArray, 0, 10);
        } catch(ArrayIndexOutOfBoundsException arrIndOutBndExp) {
            System.out.println("Arrays.sort(charArray, 0, 10): " + arrIndOutBndExp.getMessage() + "\n");  
        }
        System.out.println("stringArrary: " + Arrays.toString(stringArray));
        Arrays.sort(stringArray);
        System.out.println("Arrays.sort(stringArray): " + Arrays.toString(stringArray));
    }  
}


Output: 
byteArrary: [7, 0, 2, 6, 1, 5, 1]
Arrays.sort(byteArray): [0, 1, 1, 2, 5, 6, 7]

shortArrary: [10, 7, 12, 0, 4, 15, 20]
Arrays.sort(shortArrary, 0, 4): [0, 4, 7, 10, 12, 15, 20]

intArrary: [30, 25, 16, 26, 40, 35]
Arrays.sort(intArray, 2, 6): [30, 25, 16, 26, 35, 40]

longArrary: [111, 256, 55, 280, 180]
Arrays.sort(longArray, 0, 0): [111, 256, 55, 280, 180]

floatArrary: [3.5, 0.1, 6.2, 2.8, 4.3]
Arrays.sort(floatArray, 4, 4): [3.5, 0.1, 6.2, 2.8, 4.3]

doubleArrary: [10.15, 12.4, 7.5, 11.5, 8.9, 6.3]
Arrays.sort(doubleArray, 4, 1): fromIndex(4) > toIndex(1)

charArrary: [x, c, d, a, m, k, j]
Arrays.sort(charArray, 0, 10): Array index out of range: 10

stringArrary: [egg, doll, bat, cat, flower, apple]
Arrays.sort(stringArray): [apple, bat, cat, doll, egg, flower]

Use the social sharing button below to spread the knowledge among others.

2 comments: