The general sorting program

In the previous chapter, we implemented a simple sort algorithm. The code can sort elements of a String array. We did this to learn. For practical use, there is a ready cooked sort solution in the JDK that can sort members of collections, which are comparable.

The JDK contains a utility class called Collections. This class contains a static Collections.sort method that is capable of sorting any List that has members that are ComparableList and Comparable are interfaces defined in the JDK. Thus, if we want to sort a list of Strings, the simplest solution is as follows:

public class SimplestStringListSortTest {     @Test     public void canSortStrings() {         ArrayList actualNames = new ArrayList(Arrays.asList(  "Johnson", ...

Get Java 9 Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.