Sorting lists in Java is a common operation that helps in organizing data for easier management and better efficiency. Java provides powerful utilities and methods to sort lists in various orders, including ascending, descending, and custom orders.
In this post by The Coding College, we’ll dive into sorting techniques for lists using Java and how they can be applied in real-world scenarios.
Overview of Sorting Lists in Java
Java uses the Collections
framework and functional programming features like lambdas to sort lists efficiently. Here’s how sorting is typically done:
- Sorting in Natural Order (e.g., alphabetical or numerical ascending order).
- Sorting in Reverse Order.
- Custom Sorting using Comparators.
Example: Sorting a List in Java
To begin, import the necessary classes:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
1. Sorting in Natural Order
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
fruits.add("Cherry");
// Sorting in ascending (natural) order
Collections.sort(fruits);
System.out.println("Sorted List: " + fruits);
}
}
Output:
Sorted List: [Apple, Banana, Cherry, Mango]
2. Sorting in Reverse Order
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
fruits.add("Cherry");
// Sorting in reverse order
Collections.sort(fruits, Collections.reverseOrder());
System.out.println("Reversed List: " + fruits);
}
}
Output:
Reversed List: [Mango, Cherry, Banana, Apple]
3. Sorting Numbers
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(2);
numbers.add(9);
numbers.add(1);
// Sorting numbers in ascending order
Collections.sort(numbers);
System.out.println("Sorted Numbers: " + numbers);
}
}
Output:
Sorted Numbers: [1, 2, 5, 9]
4. Custom Sorting with Comparator
Sorting Strings by Length:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
fruits.add("Cherry");
// Custom sorting: by string length
Collections.sort(fruits, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return Integer.compare(s1.length(), s2.length());
}
});
System.out.println("Sorted by Length: " + fruits);
}
}
Output:
Sorted by Length: [Mango, Apple, Cherry, Banana]
5. Using Lambdas for Custom Sorting
Java 8 introduced lambdas, making custom sorting more concise:
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
fruits.add("Cherry");
// Custom sorting: by string length using lambda
Collections.sort(fruits, (s1, s2) -> Integer.compare(s1.length(), s2.length()));
System.out.println("Sorted by Length (Lambda): " + fruits);
}
}
6. Sorting a List of Objects
For sorting a list of objects, we use the Comparator
interface.
Example: Sorting Students by Age
import java.util.ArrayList;
import java.util.Collections;
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + " (" + age + ")";
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("Alice", 20));
students.add(new Student("Bob", 22));
students.add(new Student("Charlie", 18));
// Sorting by age
Collections.sort(students, (s1, s2) -> Integer.compare(s1.age, s2.age));
System.out.println("Sorted by Age: " + students);
}
}
Output:
Sorted by Age: [Charlie (18), Alice (20), Bob (22)]
Summary of List Sorting Methods
Method | Description |
---|---|
Collections.sort(list) | Sorts a list in natural (ascending) order. |
Collections.sort(list, Comparator) | Sorts a list based on a custom comparator. |
Collections.reverseOrder() | Sorts a list in descending order. |
Lambda Expressions | Provides concise syntax for custom comparators. |
Conclusion
Sorting lists in Java is a fundamental skill for managing data effectively. Whether sorting in natural order or using custom rules, Java provides robust tools to make the task easy and efficient.
Visit The Coding College for more in-depth tutorials and tips to enhance your Java programming journey!