Java List Sorting
Sorting data is a common task in many Java applications, whether you're
organizing names, numbers, products, or records. The Java Collections
Framework provides powerful utilities to simplify this
process.
In Java, lists such as ArrayList and LinkedList can be sorted using the
Collections class, which is available in the java.util package. The
Collections class provides the sort() method, allowing developers to
arrange elements alphabetically or numerically.
This method works with any collection that implements the List
interface.
Sorting an ArrayList in Ascending Order
The Collections.sort() method sorts elements in ascending order by
default.
- For Strings, sorting is done alphabetically.
- For numbers, sorting is done numerically.
Example: Sorting a List of Student Names
import java.util.ArrayList;
import java.util.Collections;
public class StudentSortExample {
public static void main(String[] args) {
ArrayList<String> students = new
ArrayList<>();
students.add("Ravi");
students.add("Anita");
students.add("Karan");
students.add("Divya");
// Sorting the list
alphabetically
Collections.sort(students);
System.out.println("Sorted Student
Names:");
for(String name : students) {
System.out.println(name);
}
}
}
Output Order
Anita
Divya
Karan
Ravi
The list is sorted alphabetically in ascending order.
Example: Sorting a List of Numbers
The same sort() method can be used to sort numerical values.
import java.util.ArrayList;
import java.util.Collections;
public class ScoreSortExample {
public static void main(String[] args) {
ArrayList<Integer> scores = new
ArrayList<>();
scores.add(72);
scores.add(95);
scores.add(64);
scores.add(88);
scores.add(79);
// Sorting numbers in ascending
order
Collections.sort(scores);
System.out.println("Sorted
Scores:");
for(Integer score : scores) {
System.out.println(score);
}
}
}
Output
64
72
79
88
95
Sorting a List in Descending Order
Sometimes you may want to sort elements in reverse order (descending).
This can be done
using the Collections.reverseOrder() method.
Example: Sorting Products in Reverse Alphabetical Order
import java.util.ArrayList;
import java.util.Collections;
public class ProductSortExample {
public static void main(String[] args) {
ArrayList<String> products = new
ArrayList<>();
products.add("Laptop");
products.add("Mouse");
products.add("Keyboard");
products.add("Monitor");
// Sorting in descending order
Collections.sort(products,
Collections.reverseOrder());
System.out.println("Products in Reverse
Order:");
for(String product : products) {
System.out.println(product);
}
}
}
Example: Sorting Numbers in Descending Order
import java.util.ArrayList;
import java.util.Collections;
public class PriceSortExample {
public static void main(String[] args) {
ArrayList<Integer> prices = new
ArrayList<>();
prices.add(450);
prices.add(120);
prices.add(980);
prices.add(300);
prices.add(600);
// Sorting numbers in descending
order
Collections.sort(prices,
Collections.reverseOrder());
System.out.println("Prices (High to
Low):");
for(Integer price : prices) {
System.out.println(price);
}
}
}
Key Points About List Sorting
- The Collections.sort() method sorts elements in ascending order by default.
- Sorting works for Strings, numbers, and other comparable objects.
- The Collections.reverseOrder() method is used for descending sorting.
- Sorting works for any List implementation, including:
ArrayList
LinkedList
Vector