freeCodeCamp
freecodecamp.org › news › how-to-sort-a-list-in-java
How to Sort a List in Java – Java List Sorting Example
January 24, 2023 - Here's how it works: import ... void main(String[] args) { List<Integer> numbers = Arrays.asList(5, 3, 2, 4, 1); numbers.sort(null); System.out.println(numbers); // prints [1, 2, 3, 4, 5] } }...
16:02
How To Sort A List Using Java 8 different ways - YouTube
01:07
Sort list of string using java 8 streams - YouTube
20:14
CP-1.010 Java Tutorial - Sorting an ArrayList - YouTube
05:53
Java Collection List Sort using Comparator Interface - YouTube
06:48
Java 8 Tutorial 05 :- How to sort a List in Java 8 using Comparator ...
15:58
Java 8 Stream - Sort List (ArrayList) in Ascending and Descending ...
GeeksforGeeks
geeksforgeeks.org › java › collections-sort-java-examples
Collections.sort() in Java with Examples - GeeksforGeeks
Sorts elements based on natural ordering or custom Comparator ... import java.util.*; public class Collectionsorting { public static void main(String[] args) { // Create a list of strings ArrayList<String> al = new ArrayList<String>(); al.add("Geeks For Geeks"); al.add("Friends"); al.add("Dear"); al.add("Is"); al.add("Superb"); /* Collections.sort method is sorting the elements of ArrayList in ascending order.
Published: April 30, 2026
CodeJava
codejava.net › java-core › collections › sorting-list-collections-examples
Java Sort List Collections Examples
This would be useful if we need to sort a list of custom objects which we cannot modify its class; or if we don’t want to rely on the natural ordering of the elements. The following code is example of a comparator that compares two employees based on their ages: package net.codejava.collections; import java.util.Comparator; /** * This comparator compares two employees by their ages.
W3Schools
w3schools.com › java › ref_arraylist_sort.asp
Java ArrayList sort() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A · ❮ ArrayList Methods · Sort a list in alphabetical order: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); cars.sort(null); System.out.println(cars); } } Try it Yourself » ·
Javatpoint
javatpoint.com › how-to-sort-a-list-in-java
How to Sort a List in Java - Javatpoint
How to Sort a List in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
Top answer 1 of 16
696
Collections.sort(testList);
Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Here is the documentation for Collections.
2 of 16
184
Descending:
Collections.sort(mArrayList, new Comparator<CustomData>() {
@Override
public int compare(CustomData lhs, CustomData rhs) {
// -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
return lhs.customInt > rhs.customInt ? -1 : (lhs.customInt < rhs.customInt) ? 1 : 0;
}
});
DZone
dzone.com › coding › java › java 8 comparator: how to sort a list
Java 8 Comparator: How to Sort a List - DZone
May 20, 2019 - The Premium Java Programming Certification Bundle* ... List<Integer> numbers = Arrays.asList(6, 2, 1, 4, 9); System.out.println(numbers); //[6, 2, 1, 4, 9] numbers.sort(Comparator.naturalOrder()); System.out.println(numbers); //[1, 2, 4, 6, 9] Let’s suppose we have our Movie class and we want to sort our List by title. We can use Comparator....
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-sort-an-arraylist
Java Program to Sort an ArrayList - GeeksforGeeks
July 23, 2025 - Hence, the list must have the same type of elements. Let's consider the following example to understand sorting. ... // Java Program to Sort an ArrayList // import java.util package import java.util.*; class GFG { // Main driver method public static void main(String[] args) { // Define an objects of ArrayList class ArrayList<String> list = new ArrayList<String>(); // Adding elements to the ArrayList list.add("India"); list.add("Pakistan"); list.add("Srilanka"); list.add("USA"); list.add("Australia"); list.add("Japan"); // Printing the unsorted ArrayList System.out.println("Before Sorting : " + list); // Sorting ArrayList in ascending Order Collections.sort(list); // printing the sorted ArrayList System.out.println("After Sorting : " + list); } }
Medium
medium.com › @AlexanderObregon › sorting-lists-in-java-using-collections-sort-14b150ac8682
Sorting Lists in Java Using Collections.sort | Medium
June 4, 2025 - Sorting is a basic task in any programming language, but the way it’s done can reveal a lot about how things work behind the scenes. Java provides several tools for sorting, but one of the most commonly used is Collections.sort(). It works with lists of numbers, strings, or even custom objects ...
Programiz
programiz.com › java-programming › library › arraylist › sort
Java ArrayList sort()
import java.util.ArrayList; import ... System.out.println("Sorted ArrayList: " + languages); } } ... In the above example, we have used the sort() method to sort the arraylist named languages....
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › util › list
Sorted List Java Example - Java Code Geeks
July 22, 2020 - I added a second parameter to Collections.sort method in order to tell that method how to sort the list. This is performed by @Overriding the compare method of the Comparator. My Comparator tells to the method that it should sort the list in the reversed order.
GeeksforGeeks
geeksforgeeks.org › java › sorting-in-java
Sorting in Java - GeeksforGeeks
Collections.sort() works for objects Collections like ArrayList and LinkedList. ... import java.util.*; public class GFG { public static void main(String[] args) { // Create a list of strings ArrayList<String> al = new ArrayList<String>(); al.add("Geeks For Geeks"); al.add("Friends"); al.add("Dear"); al.add("Is"); al.add("Superb"); /* Collections.sort method is sorting the elements of ArrayList in ascending order.
Published: April 16, 2026
Codecademy
codecademy.com › docs › java › collections › .sort()
Java | Collections | .sort() | Codecademy
July 18, 2022 - import java.util.*; // This sorts by natural ordering Collections.sort(myList); // This sorts by a Comparator Collections.sort(myList, myComparator); Both methods sort myList. The first sorts myList using the natural ordering of the elements.
TutorialsPoint
tutorialspoint.com › java › util › collections_sort_comparable.htm
Java Collections sort() Method
Collections.sort(list); System.out.println("Final collection value: " + list); } } Let us compile and run the above program, this will produce the following result. Initial collection value: [1, 4, 3, 6, 5, 2] Final collection value: [1, 2, ...