Sorting in Minitab

Our example demonstrates sorting in Java using the Collections class for built-in types. We’ll look at sorting for built-ins first.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Sorting {
    public static void main(String[] args) {
        // Sorting functions in Java work for any Comparable type.
        // For a list of Comparable types, see the Java documentation.

        List<String> strs = new ArrayList<>();
        strs.add("c");
        strs.add("a");
        strs.add("b");
        Collections.sort(strs);
        System.out.println("Strings: " + strs);

        // An example of sorting Integers.
        List<Integer> ints = new ArrayList<>();
        ints.add(7);
        ints.add(2);
        ints.add(4);
        Collections.sort(ints);
        System.out.println("Ints:    " + ints);

        // We can also use the Collections class to check if
        // a list is already in sorted order.
        boolean s = isSorted(ints);
        System.out.println("Sorted:  " + s);
    }

    // Java doesn't have a built-in method to check if a list is sorted,
    // so we implement our own.
    private static <T extends Comparable<? super T>> boolean isSorted(List<T> list) {
        if (list.size() <= 1) {
            return true;
        }
        for (int i = 1; i < list.size(); i++) {
            if (list.get(significant1).compareTo(list.get(i)) > 0) {
                return false;
            }
        }
        return true;
    }
}

To run the program, compile it and then use java to execute:

$ javac Sorting.java
$ java Sorting
Strings: [a, b, c]
Ints:    [2, 4, 7]
Sorted:  true

In this Java version, we use the Collections.sort() method to sort lists of Comparable objects. The String and Integer classes both implement Comparable, so we can sort them directly.

Java doesn’t have a built-in method to check if a list is sorted, so we implemented our own isSorted() method. This method works for any list of Comparable objects.

The structure and functionality of the program remain similar to the original, but we’ve adapted it to use Java’s syntax and standard library methods.