DEV Community

Cover image for Case Study: Sorting an Array of Objects
Paul Ngugi
Paul Ngugi

Posted on

Case Study: Sorting an Array of Objects

You can develop a generic method for sorting an array of Comparable objects. This section presents a generic method for sorting an array of Comparable objects. The objects are instances of the Comparable interface, and they are compared using the compareTo method. To test the method, the program sorts an array of integers, an array of double numbers, an array of characters, and an array of strings. The program is shown in the code below.

package demo;

public class GenericSort {

    public static void main(String[] args) {
        // Create an Integer array
        Integer[] intArray = {2, 4, 3};

        // Create an Double array
        Double[] doubleArray = {3.4, 1.3, -22.1};

        // Create an Character array
        Character[] charArray = {'a', 'J', 'r'};

        // Create a String array
        String[] stringArray = {"Tom", "Susan", "Kim"};

        // Sort the array
        sort(intArray);
        sort(doubleArray);
        sort(charArray);
        sort(stringArray);

        // Display the sorted arrays
        System.out.print("Sorted Integer objects: ");
        printList(intArray);
        System.out.print("Sorted Double objects: ");
        printList(doubleArray);
        System.out.print("Sorted Character objects: ");
        printList(charArray);
        System.out.print("Sorted String objects: ");
        printList(stringArray);
    }

    /** Sort an array of comparable objects */
    public static <E extends Comparable<E>> void sort(E[] list) {
        E currentMin;
        int currentMinIndex;

        for(int i = 0; i < list.length - 1; i++) {
            // Find the minimum in the list[i+1..list.length-2]
            currentMin = list[i];
            currentMinIndex = i;

            for(int j = i + 1; j < list.length; j++) {
                if(currentMin.compareTo(list[j]) > 0) {
                    currentMin = list[j];
                    currentMinIndex = j;
                }
            }

            // Swap list[i] with list[currentMinIndex] if necessary
            if(currentMinIndex != i) {
                list[currentMinIndex] = list[i];
                list[i] = currentMin;
            }
        }
    }

    /** Print an array of objects */
    public static void printList(Object[] list) {
        for(int i = 0; i < list.length; i++)
            System.out.print(list[i] + " ");
        System.out.println();
    }

}

Enter fullscreen mode Exit fullscreen mode

Sorted Integer objects: 2 3 4
Sorted Double objects: -22.1 1.3 3.4
Sorted Character objects: J a r
Sorted String objects: Kim Susan Tom

The algorithm for the sort method is the same as in SelectionSort.java. The sort method in that program sorts an array of double values. The sort method in this example can sort an array of any object type, provided that the objects are also instances of the Comparable interface. The generic type is defined as > (line 36). This has two meanings. First, it specifies that E is a subtype of Comparable. Second, it specifies that the elements to be compared are of the E type as well.

The sort method uses the compareTo method to determine the order of the objects in the array (line 46). Integer, Double, Character, and String implement Comparable, so the objects of these classes can be compared using the compareTo method. The program creates arrays of Integer objects, Double objects, Character objects, and String objects (lines 7–16) and invoke the sort method to sort these arrays (lines 19–22).

Top comments (0)