ArrayList stores elements in insertion order in java. However, in many real-world scenarios, we need to sort the elements-for example, sorting names alphabetically or numbers in increasing order. In this article, we will learn how to sort an ArrayList in ascending order in Java using built-in utilities.
Example:
Input: Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal]
Output: Sorted ArrayList: [A computer portal, For, ForGeeks, Geeks, GeeksForGeeks]
1. Using Collections.sort()
Java provides the Collections.sort() method to sort a list according to the natural ordering of its elements.
- For String objects, natural order means lexicographical (dictionary) order
- The sorting is done in-place, meaning the original list is modified
import java.util.*;
public class GFG {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Geeks");
list.add("For");
list.add("ForGeeks");
list.add("GeeksForGeeks");
list.add("A computer portal");
System.out.println("Unsorted ArrayList: " + list);
Collections.sort(list);
System.out.println("Sorted ArrayList in Ascending order: " + list);
}
}
Output
Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal] Sorted ArrayList in Ascending order: [A computer portal, For, ForGeeks, Geeks, GeeksForGeeks]
Explanation:
- An ArrayList<String> is created and populated with string values
- Collections.sort(list) sorts the elements based on their natural ordering
- The method modifies the original list and does not return a new one
- The sorted result is printed to the console
2. Using ArrayList.sort() Method
From Java 8 onwards, the List interface provides the sort() method, making sorting more object-oriented and readable.
import java.util.*;
public class GFG {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(
Arrays.asList("Geeks", "For", "ForGeeks", "GeeksForGeeks", "A computer portal")
);
list.sort(null); // natural ascending order
System.out.println("Sorted ArrayList: " + list);
}
}
Output
Sorted ArrayList: [A computer portal, For, ForGeeks, Geeks, GeeksForGeeks]
Explanation:
- sort(null) uses natural ordering
- Works similar to Collections.sort()
- Preferred in modern Java code
3. Using Collections.sort() with Comparator
A Comparator allows explicit control over sorting logic. For ascending order, we can use Comparator.naturalOrder().
import java.util.*;
public class GFG {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Geeks");
list.add("For");
list.add("ForGeeks");
Collections.sort(list, Comparator.naturalOrder());
System.out.println("Sorted ArrayList: " + list);
}
}
Output
Sorted ArrayList: [For, ForGeeks, Geeks]
Why use this?
- Makes sorting logic explicit
- Helpful when switching between ascending and descending order
4. Sorting an ArrayList of Numbers in Ascending Order
The same approaches work for numeric data types.
import java.util.*;
public class GFG {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(
Arrays.asList(5, 2, 9, 1, 3)
);
Collections.sort(numbers);
System.out.println("Sorted Numbers: " + numbers);
}
}
Output
Sorted Numbers: [1, 2, 3, 5, 9]