Here is a tutorial on how to use the java.util.Arrays class to sort Array values.
This example sorts an Array of Integers from the smallest to largest value:
import java.util.Arrays; public class HowToSortAnArray { public static void main(String[] args) { //Array of Integers int[] myIntArray = new int[]{20,100,69,4}; //SORTS ARRAY FROM SMALLEST TO LARGEST INT Arrays.sort(myIntArray); //for loop to print Array values to console for (int a = 0; a < myIntArray.length; a++) { System.out.println(myIntArray[a]); } } } //Output: 4 20 69 100
Using the same example as above but with a String Array, this will sort the values by case and then into alphabetical order:
import java.util.Arrays; public class HowToSortAnArray { public static void main(String[] args) { String[] myStrArray = new String[] {"c", "b", "a", "D"}; //SORTS STRING ARRAY INTO CASE & THEN ALPHABETICAL ORDER Arrays.sort(myStrArray); //for loop to print int values to console for (int a = 0; a < myStrArray.length; a++) { System.out.println(myStrArray[a]); } } } //Output: D a b c
To sort into alphabetical order and be case insensitive you can use:
import java.util.Arrays; public class HowToSortAnArray { public static void main(String[] args) { String[] myStrArray = new String[] {"c", "b", "a", "D"}; //SORTS ARRAY INTO ALPHABETICAL ORDER Arrays.sort(myStrArray, String.CASE_INSENSITIVE_ORDER); //for loop to print int values to console for (int a = 0; a < myStrArray.length; a++) { System.out.println(myStrArray[a]); } } } //Output: a b c D