
Sort An Array in Java
Sort an Array in Java
In Java, you can sort an array using various methods, but the most common ones are:
Using
Arrays.sort()
(from thejava.util.Arrays
class)Using a custom sorting algorithm (like Bubble Sort, Selection Sort, etc.)
Here, I'll focus on the most straightforward way using Arrays.sort()
, and I'll also provide an example of sorting an array manually using a basic sorting algorithm (Bubble Sort).
1. Sorting an Array Using Arrays.sort()
The Arrays.sort()
method is a built-in Java method that sorts arrays in ascending order by default.
Example: Sorting an Integer Array in Ascending Order
import java.util.Arrays;public class SortArray { public static void main(String[] args) { int[] arr = {5, 2, 8, 3, 1, 4}; // Sort the array using Arrays.sort() Arrays.sort(arr); // Print the sorted array System.out.println("Sorted Array: " + Arrays.toString(arr)); }}
Explanation:
The
Arrays.sort()
method sorts the array in ascending order.Arrays.toString()
is used to print the array in a readable format.
Output:
Sorted Array: [1, 2, 3, 4, 5, 8]
2. Sorting in Descending Order
By default, Arrays.sort()
sorts in ascending order. To sort in descending order, you can either use Arrays.sort()
and then reverse the array, or use a custom comparator with an Integer
array (if you are dealing with objects).
Example: Sorting in Descending Order Using Arrays.sort()
and a Comparator
import java.util.Arrays;import java.util.Collections;public class SortArrayDescending { public static void main(String[] args) { Integer[] arr = {5, 2, 8, 3, 1, 4}; // Sort the array in descending order using Arrays.sort() and Collections.reverseOrder() Arrays.sort(arr, Collections.reverseOrder()); // Print the sorted array System.out.println("Sorted Array in Descending Order: " + Arrays.toString(arr)); }}
Explanation:
This uses
Arrays.sort()
with theCollections.reverseOrder()
comparator, which sorts the array in descending order.Note: The array must be of type
Integer[]
(wrapper class) because primitive types likeint[]
do not support comparators.
Output:
Sorted Array in Descending Order: [8, 5, 4, 3, 2, 1]
3. Sorting Using a Custom Sorting Algorithm (Bubble Sort)
If you want to manually sort an array, you can implement your own sorting algorithm. One common sorting algorithm is Bubble Sort.
Example: Sorting an Array Using Bubble Sort
public class BubbleSort { public static void main(String[] args) { int[] arr = {5, 2, 8, 3, 1, 4}; // Implementing Bubble Sort for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { // Swap elements if they are in the wrong order int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } // Print the sorted array System.out.println("Sorted Array (Bubble Sort): "); for (int num : arr) { System.out.print(num + " "); } }}
Explanation:
Bubble Sort compares each element with the next one, and if they are in the wrong order, it swaps them.
It repeats the process until the array is sorted.
Output:
Sorted Array (Bubble Sort): 1 2 3 4 5 8
4. Sorting Arrays of Other Data Types
You can sort arrays of other data types (like double[]
, String[]
, etc.) in a similar way.
Example: Sorting a String Array
import java.util.Arrays;public class SortStringArray { public static void main(String[] args) { String[] arr = {"apple", "banana", "kiwi", "orange"}; // Sort the array of strings in ascending order Arrays.sort(arr); // Print the sorted array System.out.println("Sorted String Array: " + Arrays.toString(arr)); }}
Output:
Sorted String Array: [apple, banana, kiwi, orange]
Conclusion
Arrays.sort()
is the simplest and most commonly used method for sorting arrays in Java.If you want a custom sorting order (like descending), you can use
Collections.reverseOrder()
with anInteger[]
array.You can also implement custom sorting algorithms like Bubble Sort if needed.
The built-in sorting methods are highly optimized for general use, so it’s usually best to stick with them unless you have specific requirements.
Let me know if you need more examples or have any further questions!