Sorted List Example – Simple Reverse and Comparable Sort

In this Sorted List Example we will show how you can sort a java.util.List using the java.util.Collections Class. We can sort collections in Ascending or Descending order. We also show you how you can sort a collection using Java 8 Lambda expression.

Simple Sorted List Example

To sort a list in ascending order we can call the Collections.sort() method. To sort a list in descending order we call the method Collections.sort() method and pass a reverse comparable function Collections.reverseOrder() as the second parameter.

package com.memorynotfound.collections.list;

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

public class SimpleSorting {

    public static void main(String... args){
        List<Integer> random = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            random.add((int) (Math.random() * 100));
        }

        System.out.println("Initial List: " + random);

        Collections.sort(random);
        System.out.println("Sorted List: " + random);

        Collections.sort(random, Collections.reverseOrder());
        System.out.println("Reverse sorted List: " + random);
    }
}
Initial List: [93, 64, 25, 38, 91, 54, 59, 30, 87, 51]
Sorted List: [25, 30, 38, 51, 54, 59, 64, 87, 91, 93]
Reverse sorted List: [93, 91, 87, 64, 59, 54, 51, 38, 30, 25]

Comparable Sorted List Example

We can also sort a list using a custom comparator. For basic objects this is not recommend. This comparator function is interesting when you want to sort complex objects.

package com.memorynotfound.collections.list;

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

public class ComparableSorting {

    public static void main(String... args){
        List<Integer> random = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            random.add((int) (Math.random() * 100));
        }

        System.out.println("Initial List: " + random);
        Collections.sort(random, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return (o1 > o2) ? 1 : -1;
            }
        });
        System.out.println("Sorted List: " + random);

        Collections.sort(random, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return (o1 < o2) ? 1 : -1;
            }
        });
        System.out.println("Reversed Sorted List: " + random);
    }
}
Initial List: [32, 19, 77, 22, 78, 12, 72, 81, 88, 89]
Sorted List: [12, 19, 22, 32, 72, 77, 78, 81, 88, 89]
Reversed Sorted List: [89, 88, 81, 78, 77, 72, 32, 22, 19, 12]

Java 8 Lambda Sorted List Example

In Java 8 the java.util.List interface has a new sort() method. You can pass a lambda expression which will sort a collection.

package com.memorynotfound.collections.list;

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

public class LambdaSorting {

    public static void main(String... args){
        List<Integer> random = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            random.add((int) (Math.random() * 100));
        }

        System.out.println("Initial List: " + random);

        random.sort((a, b) -> (a - b));
        System.out.println("Sorted List: " + random);

        random.sort((a, b) -> (b - a));
        System.out.println("Reverse Sorted List: " + random);
    }
}
Initial List: [21, 58, 70, 38, 50, 54, 4, 16, 51, 45]
Sorted List: [4, 16, 21, 38, 45, 50, 51, 54, 58, 70]
Reverse Sorted List: [70, 58, 54, 51, 50, 45, 38, 21, 16, 4]

References

You may also like...