The Arrays.sort()
method in Java is a static method in the java.util.Arrays
class that sorts the elements of an array. It provides a convenient way to sort arrays of both primitive types and objects. The method is overloaded to handle different types of arrays, including arrays of primitive types (e.g., int
, char
, double
) and arrays of objects.
Arrays.sort()
compareTo()
method of the Comparable
interface.Here are the main overloaded variants of the Arrays.sort()
method:
public static void sort(byte[] a)
public static void sort(char[] a)
public static void sort(double[] a)
public static void sort(float[] a)
public static void sort(int[] a)
public static void sort(long[] a)
public static void sort(short[] a)
public static <T> void sort(T[] a)
public static <T> void sort(T[] a, Comparator<? super T> c)
import java.util.Arrays;
public class ArraySortExample {
public static void main(String[] args) {
int[] intArray = {5, 3, 8, 1, 2};
double[] doubleArray = {3.3, 1.1, 2.2, 5.5, 4.4};
char[] charArray = {'d', 'a', 'c', 'b', 'e'};
// Sorting primitive type arrays
Arrays.sort(intArray);
Arrays.sort(doubleArray);
Arrays.sort(charArray);
// Printing sorted arrays
System.out.println("Sorted intArray: " + Arrays.toString(intArray)); // Output: [1, 2, 3, 5, 8]
System.out.println("Sorted doubleArray: " + Arrays.toString(doubleArray)); // Output: [1.1, 2.2, 3.3, 4.4, 5.5]
System.out.println("Sorted charArray: " + Arrays.toString(charArray)); // Output: [a, b, c, d, e]
}
}
import java.util.Arrays;
public class ArraySortExample {
public static void main(String[] args) {
String[] stringArray = {"banana", "apple", "cherry", "date"};
// Sorting object type array
Arrays.sort(stringArray);
// Printing sorted array
System.out.println("Sorted stringArray: " + Arrays.toString(stringArray)); // Output: [apple, banana, cherry, date]
}
}
For object arrays, you can define a custom order by using a Comparator
.
import java.util.Arrays;
import java.util.Comparator;
public class ArraySortExample {
public static void main(String[] args) {
String[] stringArray = {"banana", "apple", "cherry", "date"};
// Sorting with a custom comparator (reverse order)
Arrays.sort(stringArray, Comparator.reverseOrder());
// Printing sorted array
System.out.println("Sorted stringArray (reverse order): " + Arrays.toString(stringArray)); // Output: [date, cherry, banana, apple]
}
}
You can also sort a subarray using the overloaded Arrays.sort()
method that accepts fromIndex
and toIndex
parameters.
import java.util.Arrays;
public class ArraySortExample {
public static void main(String[] args) {
int[] intArray = {5, 3, 8, 1, 2, 9, 4, 7, 6};
// Sorting a subarray from index 2 (inclusive) to index 7 (exclusive)
Arrays.sort(intArray, 2, 7);
// Printing sorted array
System.out.println("Partially sorted intArray: " + Arrays.toString(intArray)); // Output: [5, 3, 1, 2, 8, 9, 4, 7, 6]
}
}
In summary, Arrays.sort()
is a powerful and flexible method for sorting arrays in Java, offering various overloaded versions to handle different types of arrays and providing options for custom sorting orders through comparators.