Sort Arrays in Java using java.util.Arrays class

java.util.Arrays class has built in method to make your job easy. You can use following method to sort any array in Java.

Arrays.sort (int [])
Arrays.sort (String [])
Arrays.sort (float [])
Arrays.sort (double [])
Arrays.sort (long [])
Arrays.sort (Object [])


String [] stringArray = {"ab", "aB", "c", "0", "2", "1Ad", "a10"};
//Sort array in ascending order
Arrays.sort(stringArray);
//Sort array in reverse order
Arrays.sort(stringArray, Collections.reverseOrder());
//Sorting array starting from index 3 till 6
Arrays.sort(stringArray, 3, 6);
for (String str : stringArray) {
    System.out.println(str);
}
  

0 comments: