Arrays class provides many sort() overloaded methods and below are the few of these methods.
- static void sort(byte[] a) - Sorts the specified array into ascending numerical order.
- static void sort(byte[] a, int fromIndex, int toIndex) - Sorts the specified range of the array into ascending order.
- static void sort(char[] a) - Sorts the specified array into ascending numerical order.
- static void sort(char[] a, int fromIndex, int toIndex) - Sorts the specified range of the array into ascending order.
- static void sort(double[] a) - Sorts the specified array into ascending numerical order.
- static void sort(double[] a, int fromIndex, int toIndex) - Sorts the specified range of the array into ascending order.
- static void sort(float[] a) - Sorts the specified array into ascending numerical order.
- static void sort(float[] a, int fromIndex, int toIndex) - Sorts the specified range of the array into ascending order.
- static void sort(int[] a) - Sorts the specified array into ascending numerical order.
- static void sort(int[] a, int fromIndex, int toIndex) - Sorts the specified range of the array into ascending order.
- static void sort(long[] a) - Sorts the specified array into ascending numerical order.
- static void sort(long[] a, int fromIndex, int toIndex) - Sorts the specified range of the array into ascending order.
- static void sort(Object[] a) - Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.
- static void sort(Object[] a, int fromIndex, int toIndex) - Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.
- static void sort(short[] a) - Sorts the specified array into ascending numerical order.
- static void sort(short[] a, int fromIndex, int toIndex) - Sorts the specified range of the array into ascending order.
- static void sort(T[] a, Comparator<? super T> c) - Sorts the specified array of objects according to the order induced by the specified comparator.
- static void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) - Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
import java.util.Arrays; import java.util.Date; import java.util.List; /** * java.util.Arrays Class API examples * * @author javaguides.net * */ public class ArraysJavaUtilClass { public static void main(String[] args) { // Sorts the specified array into ascending numerical order. int[] intArray = { 3, 1, 2, 4 }; System.out.println("Original Array : " + Arrays.toString(intArray)); Arrays.sort(intArray); System.out.println("Sorted Array : " + Arrays.toString(intArray)); // Sorts the specified array into ascending numerical order. byte[] byteArray = { 1, 3, 2, 1, 5 }; System.out.println("Original Array : " + Arrays.toString(byteArray)); Arrays.sort(byteArray); System.out.println("Sorted Array : " + Arrays.toString(byteArray)); // Sorts the specified array into ascending numerical order. char[] charArray = { 'a', 'd', 'c','b' }; System.out.println("Original Array : " + Arrays.toString(charArray)); Arrays.sort(charArray); System.out.println("Sorted Array : " + Arrays.toString(charArray)); // Sorts the specified array into ascending numerical order. double[] doubleArray = { 0.1, 0.3, 0.2 }; System.out.println("Original Array : " + Arrays.toString(doubleArray)); Arrays.sort(doubleArray); System.out.println("Sorted Array : " + Arrays.toString(doubleArray)); // Sorts the specified array into ascending numerical order. long[] longArray = { 1, 3, 2, 5, 4 }; System.out.println("Original Array : " + Arrays.toString(longArray)); Arrays.sort(longArray); System.out.println("Sorted Array : " + Arrays.toString(longArray)); // Sorts the specified array into ascending numerical order. float[] floatArray = { 1.1f, 1.5f, 1.4f }; System.out.println("Original Array : " + Arrays.toString(floatArray)); Arrays.sort(floatArray); System.out.println("Sorted Array : " + Arrays.toString(floatArray)); } }
Output:
Original Array : [3, 1, 2, 4] Sorted Array : [1, 2, 3, 4] Original Array : [1, 3, 2, 1, 5] Sorted Array : [1, 1, 2, 3, 5] Original Array : [a, d, c, b] Sorted Array : [a, b, c, d] Original Array : [0.1, 0.3, 0.2] Sorted Array : [0.1, 0.2, 0.3] Original Array : [1, 3, 2, 5, 4] Sorted Array : [1, 2, 3, 4, 5] Original Array : [1.1, 1.5, 1.4] Sorted Array : [1.1, 1.4, 1.5]
Reference
Array Java
Comments
Post a Comment