Java

Java Collections Sort and Arrays Sort

ArrayList and the Vector are each examples of a list in Java. There are other types of lists. A Collections class has the sort() method to sort a list in ascending order. It also has the reverseOrder() method, which enables sorting in descending (reverse) order. The reverseOrder method is not used ordinarily. It is used as an argument in one of the overloaded sort() methods. The Collections class is in the java.util.* package, which has to be imported by the programmer to be used.

Consider the following unsorted list of sets of people:

children, boys, groups, girls, aunts, uncles, parents

If this list is sorted in dictionary ascending order, the result would be:

aunts, boys, children, girls, groups, parents, uncles

If the list is sorted in descending order, then the result would be:

uncles, parents, groups, girls, children, boys, aunts

The full syntaxes for the main Collections sorting methods are:

public static <T extends Comparable<? super T>> void sort(List<T> list)

and

public static <T> void sort(List<T> list, Comparator<? super T> c)

The full syntax for the reverseOrder method is:

public static <T> Comparator<T> reverseOrder()

The reverseOrder() method is used with the second method above. “static” means that the Collections class does not have to be instantiated to use the method.

The normal array, too, can be sorted. Implemented lists need the Collections class for sorting. The array needs the Arrays class for sorting. The sorting methods of the Arrays class that correspond to the above sorting methods are:

public static void sort(Object[] a)

and

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

The same reverseOrder() method is used with the second method here for reversing.

The Arrays class is also in the java.util.* package, and has to be imported.

Sort Ascending

The first form of the two overloaded sort methods above is used for sorting in ascending order.

Sorting ArrayList Ascending

The sort method returns void. The following program shows how the ArrayList is sorted, in ascending order:

import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add("children"); al.add("boys"); al.add("groups"); al.add("girls");
al.add("aunts"); al.add("uncles"); al.add("parents");
Collections.sort(al);
            for (int i=0; i<al.size(); i++) {
System.out.print(al.get(i)); System.out.print(' ');
            }
System.out.println();
        }
    }

The output is:

aunts boys children girls groups parents uncles

Sorting Vector Ascending

The sort method returns void. The following program shows how the Vector is sorted, in ascending order:

import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            Vector v = new Vector();
v.add("children"); v.add("boys"); v.add("groups"); v.add("girls");
v.add("aunts"); v.add("uncles"); v.add("parents");
Collections.sort(v);
            for (int i=0; i<v.size(); i++) {
System.out.print(v.get(i)); System.out.print(' ');
            }
System.out.println();
        }
    }

The output is:

aunts boys children girls groups parents uncles

Sorting array type [] Ascending

The sort method returns void. The following program shows how the ordinary array is sorted in ascending order:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
String[] arr = new String[] {"children", "boys", "groups", "girls", "aunts", "uncles", "parents"};
Arrays.sort(arr);
            for (int i=0; i<arr.length; i++) {
System.out.print(arr[i]); System.out.print(' ');
            }
System.out.println();
        }
    }

The output is:

aunts boys children girls groups parents uncles

Sort Descending

Collections and Arrays are actually two different classes. Arrays have two sort() overloaded methods, similar to the overloaded sort() methods of Collections, given above. For both sort schemes, the reverseOrder() method of the collection class returns a comparator object, to be used as a second argument, for one of the sort methods, for descending order. The syntax to use is:

Collections.reverseOrder()

Sorting ArrayList Descending

The overloaded sort method with a second argument is used to sort descending. The expression “Collections.reverseOrder()” should be used for the second argument. The following program shows how the ArrayList is sorted, in descending order:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add("children"); al.add("boys"); al.add("groups"); al.add("girls");
al.add("aunts"); al.add("uncles"); al.add("parents");
Collections.sort(al, Collections.reverseOrder());
            for (int i=0; i<al.size(); i++) {
System.out.print(al.get(i)); System.out.print(' ');
            }
System.out.println();
        }
    }

The output is:

uncles parents groups girls, children, boys aunts

Sorting Vector Descending

The overloaded sort method with a second argument is used to sort descending. The expression “Collections.reverseOrder()” should be used for the second argument. The following program shows how the Vector is sorted, in descending order:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            Vector v = new Vector();
v.add("children"); v.add("boys"); v.add("groups"); v.add("girls");
v.add("aunts"); v.add("uncles"); v.add("parents");
Collections.sort(v, Collections.reverseOrder());
            for (int i=0; i<v.size(); i++) {
System.out.print(v.get(i)); System.out.print(' ');
            }
System.out.println();
        }
    }

The output is:

uncles parents groups girls, children, boys aunts

Sorting array type [] Descending

The overloaded sort method for Arrays, with a second argument, is used to sort descending. The expression “Collections.reverseOrder()” should be used for the second argument. The following program shows how the ordinary array is sorted, in descending order:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
String[] arr = new String[] {"children", "boys", "groups", "girls", "aunts", "uncles", "parents"};
Arrays.sort(arr, Collections.reverseOrder());
            for (int i=0; i<arr.length; i++) {
System.out.print(arr[i]); System.out.print(' ');
            }
System.out.println();
        }
    }

The output is:

uncles parents groups girls, children, boys aunts

Conclusion

ArrayList and the Vector are each examples of a list in Java. There are other types of lists. A Collections class has the sort() method to sort a list in ascending order. It also has the reverseOrder() method, which enables sorting in descending (reverse) order. The reverseOrder method is not used in an ordinary way. It is used as an argument in one of the overloaded sort() methods. The Collections class is in the java.util.* package, which has to be imported by the programmer to be used.

The Arrays class has many overloaded sort methods. Two of them are:

public static void sort(Object[] a)

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

The Collections class has correspondingly two overloaded sort methods, which are:

public static <T extends Comparable<? super T>> void sort(List<T> list)

public static <T> void sort(List<T> list, Comparator<? super T> c)

The first method of the Arrays class sorts an array of objects, ascending. The first method of the Collections class sorts a list of objects, ascending. To sort descending, both second methods here are configured in the same way, for their second arguments, i.e. Collections.reverseOrder().

Java predefined list examples are ArrayList, AttributeList, LinkedList, Stack, and Vector. Arrays sort arrays, while Collections sorts lists.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.