En este ejemplo, estamos ordenando una matriz de caracteres. Hemos demostrado dos tipos de clasificación en el programa 1) Clasificación completa usando sort (char[] a) método 2) Ordenar el rango especificado de caracteres usando solo sort (char[] a, int fromIndex, int toIndex) método.
import java.util.Arrays; class SortCharArray { public static void main(String[] args) { // Creating a Char Array char[] charArray = new char[] { 'A', 'Q', 'S', 'Z', 'P' }; // Displaying Array before Sorting System.out.println("**Char Array Before Sorting**"); for (char ch: charArray){ System.out.println(ch); } // Sorting the Array Arrays.sort(charArray); System.out.println("**Char Array After Sorting**"); for (char ch: charArray){ System.out.println(ch); } // Another Char Array char[] charArray2 = new char[] { 'D', 'F', 'V', 'J', 'U', 'M', 'C' }; // Selective Sorting /* public static void sort(char[] a, int fromIndex, * int toIndex): Sorts the specified range of the * array into ascending order. The range to be sorted * extends from the index fromIndex, inclusive, to the * index toIndex, exclusive. If fromIndex == toIndex, * the range to be sorted is empty. */ Arrays.sort(charArray2, 2, 5); // Displaying array after selective sorting System.out.println("**Selective Sorting**"); for (char ch: charArray2){ System.out.println(ch); } } }
Producción:
**Char Array Before Sorting** A Q S Z P **Char Array After Sorting** A P Q S Z **Selective Sorting** D F J U V M C