PDA

View Full Version : standard sort array



asylu3
19-04-2004, 02:15 PM
You should import java.util.Arrays in order to do this.

int[] intArray = new int[] {4, 1, 3, -23};
Arrays.sort(intArray);
// [-23, 1, 3, 4]

String[] strArray = new String[] {"z", "a", "C"};
Arrays.sort(strArray);
// [C, a, z]

// Case-insensitive sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
// [a, C, z]

// Reverse-order sort
Arrays.sort(strArray, Collections.reverseOrder());
// [z, a, C]

// Case-insensitive reverse-order sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(strArray));
// [z, C, a]