π’ Java: How to Sort an Array (Ascending & Descending)
π§² Introduction β Why Sorting Arrays Matters
Sorting is one of the most frequent and critical operations in programming. Whether you’re organizing search results, ranking scores, or optimizing data analysis, sorting arrays is a must-have skill.
β In this guide, you’ll learn:
- How to sort integer, string, and custom arrays in Java
- Sort in ascending and descending order
- Use of
Arrays.sort()andCollections.reverseOrder() - Best practices for performance and correctness
π Required Import
import java.util.Arrays;
import java.util.Collections;
β Explanation:
Arrays.sort()is used for sorting arrays.Collections.reverseOrder()is used to sort arrays in descending order (only for object arrays).
πΌ Method 1: Sort an Integer Array in Ascending Order
import java.util.Arrays;
public class SortIntAsc {
public static void main(String[] args) {
int[] numbers = {50, 10, 30, 20, 40};
Arrays.sort(numbers);
System.out.println("Sorted Array (Ascending): " + Arrays.toString(numbers));
}
}
β Explanation:
Arrays.sort()arranges elements in ascending order.- Works with primitive types like
int,char, etc.
π½ Method 2: Sort Integer Array in Descending Order
import java.util.Arrays;
import java.util.Collections;
public class SortIntDesc {
public static void main(String[] args) {
Integer[] numbers = {50, 10, 30, 20, 40};
Arrays.sort(numbers, Collections.reverseOrder());
System.out.println("Sorted Array (Descending): " + Arrays.toString(numbers));
}
}
β Explanation:
- You must use
Integer[]instead ofint[]becauseCollections.reverseOrder()works with objects, not primitives.
π‘ Tip: Use Integer[] if you need reverse sorting.
π€ Method 3: Sort a String Array Alphabetically
import java.util.Arrays;
public class SortStrings {
public static void main(String[] args) {
String[] names = {"Zara", "Mike", "Alex", "Bob"};
Arrays.sort(names);
System.out.println("Sorted Names: " + Arrays.toString(names));
}
}
β Explanation:
Arrays.sort()uses lexicographical order (A-Z).
π Method 4: Sort a String Array in Reverse Order
import java.util.Arrays;
import java.util.Collections;
public class SortStringsDesc {
public static void main(String[] args) {
String[] names = {"Zara", "Mike", "Alex", "Bob"};
Arrays.sort(names, Collections.reverseOrder());
System.out.println("Sorted Names (Descending): " + Arrays.toString(names));
}
}
β Explanation:
- Reverse alphabetical sorting using
Collections.reverseOrder().
π§ Method 5: Sort Part of an Array
int[] partial = {10, 5, 30, 25, 40, 35};
Arrays.sort(partial, 1, 5); // Sorts elements from index 1 to 4
β Explanation:
- Only sorts elements from index
1(inclusive) to5(exclusive).
π Performance Note
| Array Type | Method | Sort Time Complexity |
|---|---|---|
int[], double[] | Arrays.sort() | O(n log n) |
Integer[], String[] | Arrays.sort() | O(n log n) (uses TimSort) |
π‘ Note: For large-scale data, consider parallelSort() introduced in Java 8.
π‘ Best Practices
- β
Use
Arrays.sort()for quick and clean sorting - β
Use
Collections.reverseOrder()only with object arrays - β οΈ Avoid converting
int[]toInteger[]unless necessary - β
Use
Arrays.toString()to print arrays cleanly
π Summary β Sorting Arrays in Java
Sorting is a core Java skill used in virtually every application domain. With Arrays.sort() and helper classes, Java makes it easy to sort data quickly and efficiently.
π§Ύ Key Takeaways:
- Use
Arrays.sort()for most sorting needs. - Use
Collections.reverseOrder()for descending sort (object arrays). - Use
Arrays.toString()to print sorted arrays. - Optimize performance by avoiding unnecessary boxing/unboxing.
βFAQs β Sort an Array in Java
β How to sort an array in Java?
Use Arrays.sort(array) for ascending order.
β How to sort an array in descending order?
Convert to Integer[] or String[], then use:
Arrays.sort(array, Collections.reverseOrder());
β Can I sort only part of an array?
Yes, use Arrays.sort(array, fromIndex, toIndex).
β Whatβs the difference between int[] and Integer[]?
int[] is primitive and doesnβt support object-based operations like reverse sorting.
β Is sorting stable in Java?
Yes, for object arrays Java uses TimSort, which is stable.
Share Now :
