How to Sort a List in Java?

In today’s article, we will learn a few methods to sort a list in Java. Sorting means arranging the items in ascending or descending order. So come on! Let us start with the topic. But first let us see what is a list in java?

List in Java

The Collection framework is a boon in java programming. It makes the data operations like searching, sorting, insertion, manipulation, and deletion easier and more effective. And also it helps us to implement all the data structures with ease. The list is a part of the collection frameworks in Java. At runtime List can expand or shrink based on the number of data given. In simple terms, the list is a dynamic data structure in Java. So there is no need to provide the size initially. So there is no memory wastage.

Methods to Sort a List in Java:

There are five methods to sort a list. They are listed below.

  • Group 1
    • Collections.sort() method.
    • Collections.reverseOrder() method.
  • Group 2
    • The sorted() method.
    • Comparator.reverseOrder() method.
  • Group 3
    • Comparator.naturalOrder() method.

methods to sort a list in java

Group 1:

You need to use the Collections.sort(list) method to sort the list in ascending order. If you want to arrange the list in descending order, use Collections.reverseOrder() method along with the Collections.sort(list, Comparator) method. Let us move on to know more in detail.

1. Collections.sort() method -Ascending order:

The Collections.sort() method is a part of Java.util.Collections class. This method organises the list in ascending order. It uses ASCII values to sort the list.

Syntax:

Collections.sort(List);

Take a look at the code below. It arranges alphabetical values in ascending order.

You can also sort numeric values in ascending order. Consider the sample code mentioned below.

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

public class ListSort {
public static void main(String[] args) {
    List<String> FirstCodeList = new ArrayList();
    Scanner scan= new Scanner(System.in);
    System.out.println("Welcome to FirstCode!");
    // Getting the list size.
    System.out.println("Enter the capacity of the list:");
    int capacity= scan.nextInt();
    //Getting the list values.
    System.out.println("Enter the values to be sorted:");
    for(int i=0; i<capacity;i++) {
        String value= scan.next();
        FirstCodeList.add(value);
    }
    System.out.println("The given list is"+ FirstCodeList);
    // Sorting the list.
    Collections.sort(FirstCodeList);
    System.out.println("The sorted list is"+ FirstCodeList);
    
}
}

Output:

Welcome to FirstCode!
Enter the capacity of the list:
5
Zebra
Yak
Carrot
Ball
Apple
The given list is [Zebra, Yak, Carrot, Ball, Apple]
The sorted list is [ Apple, Ball, Carrot, Yak, Zebra]

You can also sort numeric values in ascending order. Consider the sample code mentioned below.

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

public class ListSort {
public static void main(String[] args) {
    List<Integer> FirstCodeList = new ArrayList();
    System.out.println("Welcome to FirstCode!");
    //Adding values to the list.
    FirstCodeList.add(9);
    FirstCodeList.add(8);
FirstCodeList.add(7);
FirstCodeList.add(6);
FirstCodeList.add(5);
FirstCodeList.add(4);
FirstCodeList.add(3);
FirstCodeList.add(2);
FirstCodeList.add(1);
    System.out.println("The provided list is"+ FirstCodeList);
    //Sorting the list.
Collections.sort(FirstCodeList);
    System.out.println("The sorted list is"+ FirstCodeList);
    
}
}

Output:

Welcome to FirstCode!
The provided list is[9, 8, 7, 6, 5, 4, 3, 2, 1]
The sorted list is[1, 2, 3, 4, 5, 6, 7, 8, 9]

The above method works well for both alphabets and numbers.

2. Collections.reverseOrder() method -Descending order:

The Collections.reverseOrder() method returns a comparator for sorting in reverse order. We will use that return value as one of the arguments for the Collections.sort(List, Comparator) method. This method works for both numeric and alphabetical values.

Syntax:

Collections.sort(list, comparator);

This sorting method will sort the list in accordance to the order induced by the specified comparator.

Collections.sort(list, Collections.reverseOrder());

Take a look at the below code. It arranges alphabetical values in descending order.

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

public class ListSort {
public static void main(String[] args) {
    List<String> FirstCodeList = new ArrayList();
    Scanner scan= new Scanner(System.in);
    System.out.println("Welcome to FirstCode!");
    //Getting the list size.
    System.out.println("Enter the capacity of the list:");
    int capacity= scan.nextInt();
    //Getting the list values.
    System.out.println("Enter the values to be sorted:");
    for(int i=0; i<capacity;i++) {
        String value= scan.next();
        FirstCodeList.add(value);
    }
    System.out.println("The given list is"+ FirstCodeList);
    //Sorting the list.
    Collections.sort(FirstCodeList, Collections.reverseOrder());
    System.out.println("The sorted list is"+ FirstCodeList);
    
}
}

Output:

Welcome to FirstCode!
Enter the capacity of the list:
5
Apple
Ball
Carrot
Yak
Zebra
The given list is[ Apple, Ball, Carrot, Yak, Zebra]
The sorted list is[Zebra, Yak, Carrot, Ball, Apple]

For arranging numeric values in descending order, Consider the code mentioned below.

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

public class ListSort {
public static void main(String[] args) {
    List<Integer> FirstCodeList = new ArrayList();
    Scanner scan= new Scanner(System.in);
    System.out.println("Welcome to FirstCode!");
    //Getting the list size.
    System.out.println("Enter the capacity of the list:");
    int capacity= scan.nextInt();
    //Getting the list values.
    System.out.println("Enter the values to be sorted:");
    for(int i=0; i<capacity;i++) {
        int value= scan.nextInt();
        FirstCodeList.add(value);
    }
    System.out.println("The provided list is"+ FirstCodeList);
    //Sorting the list.
    Collections.sort(FirstCodeList, Collections.reverseOrder());
    System.out.println("The sorted list is"+ FirstCodeList);
    
}
}

Output:

Welcome to FirstCode!
Enter the capacity of the list:
5
1
2
3
4
5
The provided list is[1, 2, 3, 4, 5]
The sorted list is[ 5, 4, 3, 2, 1]

Comparator in Java:

A Comparator is an interface for comparing values based on the given criteria. The return value of the comparator decides whether a number is greater or smaller than the other one. There are three types of return values that are:

  1. The negative value means the first one is smaller than the second one.
  2. Zero means both values are equal.
  3. A positive value means the first value is larger than the second value.

For Example:

In the Collections.reverseOrder() method, The comparator is designed in such a way that it returns values to arrange in reverse order. The sort metho`d arranges the list based on the comparator.

Group 2:

Group 2 consists of two methods. Use the sorted() method to sort the list in ascending order. If you want to arrange the list in descending order, use Comparator.reverseOrder() method along with the sorted(Comparator) method. Let us proceed to know more in detail.

1. The sorted() method – Ascending order:

The sorted() method is a part of the Java.util.Stream class. This method organises the list in ascending order. It also uses ASCII values to sort the list. This method works well for both alphabets and numbers.

Syntax:

List<type> listName= listName[list to be sorted].stream().sorted().collect(Collectors.toList());

Take a look at the below code. It arranges the given values in ascending order

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class ListSort {
public static void main(String[] args) {
    List<String> FirstCodeList = new ArrayList();
    Scanner scan= new Scanner(System.in);
    System.out.println("Welcome to FirstCode!");
    //Getting the list size.
    System.out.println("Enter the capacity of the list:");
    int capacity= scan.nextInt();
    //Getting the list values.
    System.out.println("Enter the values to be sorted:");
    for(int i=0; i<capacity;i++) {
        String value= scan.next();
        FirstCodeList.add(value);
    }
    System.out.println("The given list is"+ FirstCodeList);
    //Sorting the list.
List<String> sortedList= FirstCodeList.stream().sorted().collect(Collectors.toList());
    System.out.println("The sorted list is"+ sortedList);
    
}
}

Output:

Welcome to FirstCode!
Enter the capacity of the list:
5
E
D
C
B
A
The given list is [E, D, C, B, A]
The sorted list is[A, B, C, D, E]

2. Comparator.reverseOrder() method – Descending order:

The Comparator.reverseOrder() method is a part of the Comparator interface. It is passed as an argument in the sorted() method to arrange the list in descending order. The above method works fine for both alphabets and numbers.

Syntax:

List<type> listName= listName[list to be sorted].stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

Take a look at the below code. It arranges the given values in descending order.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class ListSort {
public static void main(String[] args) {
    List<String> FirstCodeList = new ArrayList();
    Scanner scan= new Scanner(System.in);
    System.out.println("Welcome to FirstCode!");
    //Getting the list size.
    System.out.println("Enter the capacity of the list:");
    int capacity= scan.nextInt();
    //Getting the list values.
    System.out.println("Enter the values to be sorted:");
    for(int i=0; i<capacity;i++) {
        String value= scan.next();
        FirstCodeList.add(value);
    }
    System.out.println("The given list is"+ FirstCodeList);
    //Sorting the list.
List<String> sortedList= FirstCodeList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
    System.out.println("The sorted list is"+ sortedList);
    
}
}

Output:

Welcome to FirstCode!
Enter the capacity of the list:
5
Apple
Ball
Carrot
Yak
Zebra
The given list is[Apple, Ball, Carrot, Yak, Zebra]
The sorted list is[Zebra, Yak, Carrot, Ball, Apple]

Group3:

1. Comparator.naturalOrder() method:

This method arranges the elements in natural order or ascending order. We pass this method as an argument to the sort() method in the List class. The above method works fine for both alphabets and numbers.

Syntax:

list_name.sort(Comparator.naturalOrder());

Take a look at the below code. It arranges alphabetical values in ascending order.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class ListSort {
public static void main(String[] args) {
    List<String> FirstCodeList = new ArrayList();
    Scanner scan= new Scanner(System.in);
    System.out.println("Welcome to FirstCode!");
    //Getting the list size.
    System.out.println("Enter the capacity of the list:");
    int capacity= scan.nextInt();
    //Getting the list values.
    System.out.println("Enter the values to be sorted:");
    for(int i=0; i<capacity;i++) {
        String value= scan.next();
        FirstCodeList.add(value);
    }
    System.out.println("The given list is"+ FirstCodeList);
    //Sorting the list.
    FirstCodeList.sort(Comparator.naturalOrder());
    System.out.println("The sorted list is"+ FirstCodeList);
    
}
}

Output:

Welcome to FirstCode!
Enter the capacity of the list:
5
E
D
C
B
A
The given list is[E, D, C, B, A]
The sorted list is[A, B, C, D, E]

Points to remember:

1. Generally, there are three methods to sort a list in java. They are:

  • Collections.sort(list); – present in Collections class.
  • Collections.sort(list, Comparator); – present in Collections class.
  • list_name.sort(); – present in List class.

2. If the list consists of both capital and small alphabets, In ascending order, the methods arrange the capital alphabets first, followed by the small ones. In descending order, it arranges the small alphabets first, followed by the capital ones.

3. If the list has Capital letters, Small letters, and numbers, In ascending order, the methods arrange the numbers first, followed by capital alphabets, and then by the small ones. In descending order, it arranges numbers first, followed by the small alphabets, and then by the capital ones.

4. All the above methods work well for both alphabets and numbers.

Summary

In this article, we went through the java methods to sort a list in ascending and descending order. It might seem harder, but regular practice and good understanding will make you a master of Java programming. Thank you, folks! Have a happy programming journey.

Leave a Reply

Your email address will not be published. Required fields are marked *