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);
}
}

You can also try this code with Online Java Compiler
Run Code
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);
}
}

You can also try this code with Online Java Compiler
Run Code
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);

You can also try this code with Online Java Compiler
Run CodeOutput
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
Frequently Asked Questions
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++.
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.
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.
What are the different implementations of Lists in Java?
Different implementations of Lists in Java are ArrayLists, LinkedList, Vectors, Stacks, etc.
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.
Conclusion
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 -