Java LinkedList sort() Method with Examples

In the previous article, we have discussed about Java LinkedList listIterator() Method with Examples

In this article we are going to see the use of Java LinkedList sort() method along with suitable examples.

Java LinkedList sort() Method with Examples

This java.util.LinkedList.sort() method is used to the elements in ascending or descending order within the LinkedList.

If the LinkedList contains both small letter and capital letter then it sorts capital letter first and then to small letter (in case of ascending order). If the list contains null element, then it sorts the null element first (in case of ascending order).

When we compare null values it throws NullPointerException.

Let’s see sort() method with suitable examples.

Method-1: sort(comparator.naturalOrder())

Syntax:

LinkedListName.sort(comparator.naturalOrder())

Where,

  • LinkedListName refers to the name of your LinkedList.
  • sort(comparator.naturalOrder()) refers to sort the elements of the LinkedList in ascending order using a comparator which will compare the elements inside the LinkedList

Let’s see an example with String Type LinkedList to understand it more clearly.

Example: Java LinkedList sort() Method – Example with String Type LinkedList

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then sort the elements in ascending order within the LinkedList using sort(comparator.naturalOrder()) method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("A");
        l.add("b");
        l.add("D");
        l.add("e");
        l.add("F");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // sorting the elements in ascending order
        l.sort(Comparator.naturalOrder());
        // Prints the new LinkedList elements
        System.out.println("The sorted elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [A, b, D, e, F]
The sorted elements of LinkedList are: [A, D, F, b, e]

Method-2: sort(comparator.reverseOrder())

Syntax:

LinkedListName.sort(comparator.reverseOrder())

Where,

  • LinkedListName refers to the name of your LinkedList.
  • sort(comparator.reverseOrder()) refers to sort the elements of the LinkedList in descending order using a comparator which will compare the elements inside the LinkedList.

Let’s see an example with Integer Type LinkedList to understand it more clearly.

Example: Java LinkedList sort() Method – Example with Integer Type LinkedList

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then sort the elements in descending order within the LinkedList using sort(comparator.reverseOrder()) method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        l.add(1);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // sorting the elements in descending order
        l.sort(Comparator.reverseOrder());
        // Prints the new LinkedList elements
        System.out.println("The sorted elements of LinkedList are: "+l);
    }
}

Output:

The elements of LinkedList are: [2, 52, 13, 17, 1]
The sorted elements of LinkedList are: [52, 17, 13, 2, 1]

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: