Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
List in Java
3.
Java List Sort
3.1.
Sort List of Strings 
3.2.
Sort List of Objects
3.3.
Sort List using a Comparator
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

How to Sort List in Java

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Data structure and algorithm is the key to clear interviews of product-based companies. One of such important data structures is List in Java. Let us learn about Java List Sort and how to sort list in Java with Collections sort in the below article.

List in Java

List in Java stores methods to insert, delete, update, and search the elements, which are indexed based to maintain the ordered collection. They also allow duplicate and null elements to be inserted, unlike sets in C++.

The package required in Java for using the list interface is java.util. It can be thought of as a source of ListIterators, with the use of which we can iterate through the list in both forward and backward directions. LinkedList, ArrayList, Vectors, and Stacks are applications of lists. 

Let us learn different methods for Java List Sort.

Must Read Difference between ArrayList and LinkedList , and Duck Number in Java.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Java List Sort

The most popular method to define how to sort list in Java is Collections.sort(). The only requirement for sorting using this method is that all the elements in the list must implement a comparable interface.  

Java list sort is an important concept in the way to learn Java. The question ‘how to sort list in Java’ will arise in front of us many times. Let's look into examples of sorting different types of lists using the Collections.sort() method.

Sort List of Strings 

In the term ‘Java List Sort’, the Java List stands for all the types of lists. One of such important lists is a list of strings.

Sorting a list of strings with the Collections.sort() method, as the string class implements Comparable interface:

 

package com.journaldev.sort;

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

// This class shows how to sort list in Java storing strings.
public class stringListSort {

      // Main Function.
      public static void main(String[] args) {
            List<String> stringList = new ArrayList<String>();
            stringList.add("abc");
            stringList.add("cde");
            stringList.add("efg");
            stringList.add("bcd");

            //Using Collections sort method for Java List Sort.
            Collections.sort(stringList);
 
            for (String s : stringList)
                  System.out.print(" " + s);
      }
}


Output

abc bcd cde efg 

Sort List of Objects

When we are learning about Java list sort, then the list of objects must be covered. Here we will be sorting a list of custom objects with the Collections.sort() method. We can only use this method when the class implements a Comparable interface.

 

package com.journaldev.sort;

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

// Defining a new class for the object.
class objData implements Comparable<objData> {

      private int val;

      public objData(int i) {
            this.val = i;
      }

      @Override
      public int compareTo(objData d) {
            return this.val - d.getVal();
      }

      @Override
      public String toString() {
            return "objData{" + this.val + "}";
      }

      public int getVal() {
            return val;
      }
}

// This class shows how to sort list in Java storing Objects.
public class objectListSort {

      // Main Function.
      public static void main(String[] args) {
            List<objData> objList = new ArrayList<>();

            // Inserting Data.
            objList.add(new objData(2));
            objList.add(new objData(3));
            objList.add(new objData(1));

            // Printing and Sorting.
            System.out.println("List Before Sorting::" + objList);

// Java List Sort with Collections sort.
            Collections.sort(objList);

            System.out.println("List After Sorting::" + objList);

      }

}


Output

List Before Sorting::[objData{2}, objData{3}, objData{1}]
List After Sorting::[objData{1}, objData{2}, objData{3}]

 

You can easily run this code yourself with java online compiler.

Sort List using a Comparator

We can overload the Collections sort method and provide our own Comparator implementation to sort a list with our defined conditions. To write the Comparator implementation in a single line, we normally use lambda expressions as Comparator is a functional interface.

// Java List Sort with Collections Sort comparator.
Collections.sort(objList, (l1, l2) -> {
      return l2.getVal() - l1.getVal();
});
System.out.println("Using Comparator, object list in decreasing order::" + objList);

Output

Using Comparator, object list in decreasing order::[objData{3}, objData{2}, objData{1}]


Also check out Addition of Two Numbers in Java here.

Must Read Array of Objects in Java

FAQs

  1. Define lists in Java.

List in Java stores methods to insert, delete, update, and search the elements that are indexed based on maintaining the ordered collection. They also allow duplicate and null elements to be inserted, unlike sets in C++.

2. What do you understand by Java List Sort?

Java List Sort means rearranging the elements of the lists such that they are in increasing order according to some property.

3. What is the most used method for how to sort list in Java?

The most popular method to sort lists in Java is Collections sort. 

4. What are the different implementations of Lists in Java?

Different implementations of Lists in Java are ArrayLists, LinkedList, Vectors, Stacks, etc.

5. What is the package required in Java for using the List interface? 

The package required in Java for using the list interface is java.util.

Key Takeaways

In the above article, we learned about Java List Sort and how to sort list in Java. Mainly we focused on the most used method, i.e., the Collections sort method.

Recommended Problems - 


If you want to learn more about such concepts, head over to our library section for many such interesting blogs. Keep learning.

Happy Coding!

Previous article
Java Program to Find the Largest of Three Numbers
Next article
Print Duplicate Elements in Array
Live masterclass