Sort a List in Java

1. Introduction

Sorting a List is a fundamental operation in programming, allowing data to be ordered in a specific sequence (e.g., ascending or descending). In Java, the Collections framework provides several methods to sort a List, making it easier to manage and manipulate collections of data. This blog post will explore how to sort a List in Java, demonstrating both natural order and custom order sorting.

2. Program Steps

1. Create a List containing elements to be sorted.

2. Sort the List in natural order using the Collections.sort() method.

3. Define a custom Comparator to sort the List in a custom order.

4. Sort the List using the custom Comparator.

5. Display the sorted Lists to illustrate the sorting operations.

3. Code Program

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

public class SortList {
    public static void main(String[] args) {
        // Creating a List of integers
        List<Integer> numbers = new ArrayList<>();
        numbers.add(5);
        numbers.add(1);
        numbers.add(4);
        numbers.add(2);
        numbers.add(3);

        // Sorting the List in natural order
        Collections.sort(numbers);
        System.out.println("Sorted in natural order: " + numbers);

        // Creating a List of Strings
        List<String> fruits = new ArrayList<>();
        fruits.add("Banana");
        fruits.add("Apple");
        fruits.add("Cherry");
        fruits.add("Date");

        // Sorting the List in custom order (alphabetical order ignoring case)
        fruits.sort(String.CASE_INSENSITIVE_ORDER);
        System.out.println("Sorted in custom order (case insensitive): " + fruits);
    }
}

Output:

Sorted in natural order: [1, 2, 3, 4, 5]
Sorted in custom order (case insensitive): [Apple, Banana, Cherry, Date]

Explanation:

1. The program starts by importing the necessary classes from the java.util package. These include ArrayList, Collections, Comparator, and List classes.

2. A List of integers named numbers is created and populated with unsorted values. The List is then sorted in natural order (ascending) using the Collections.sort() method. The sorted List is printed to demonstrate the natural order sorting.

3. A List of Strings named fruits is created and populated with unsorted fruit names. This List is sorted in a custom order, which in this case is alphabetical order ignoring case sensitivity. This is achieved by passing String.CASE_INSENSITIVE_ORDER to the sort() method of the List.

4. The Collections.sort() method is a static method that sorts the specified list into ascending order according to the natural ordering of its elements. For custom order sorting, Lists have a sort() method that accepts a Comparator. In this example, String.CASE_INSENSITIVE_ORDER is used as a Comparator to sort strings in a case-insensitive manner.

5. The output demonstrates the sorted Lists, showing the effectiveness of both natural order sorting and custom order sorting using a Comparator in Java.

Comments