The Sort() method is used to sort a String Array in Ascending order.
import java.util.Arrays; public class MyApplication { public static void main(String[] args) { String[] arr = {"Mohan", "John", "Paul", "Kriti", "Salim"}; Arrays.sort(arr); System.out.println("The Sorted Array in ascending order is : "); for (String str : arr) { System.out.println(str); } } }
So, in the above code we have created a Array and initialised to the variable arr.
String[] arr = {"Mohan", "John", "Paul", "Kriti", "Salim"};
Below is how the values are positioned in the Array,
Then we have used the Arrays.sort(arr) method to sort the Array arr in ascending order.
Arrays.sort(arr);
And the Array arr gets sorted with John as the first value, Kriti second and Mohan as the third, Paul as fourth and Salim as the fifth value.
And we get the below output.
Even here the sort() method is used to sort the numbers in Increasing Order.
import java.util.Arrays; public class MyApplication { public static void main(String[] args) { int[] arr = {5, 3, 2, 4}; Arrays.sort(arr); System.out.println("The Sorted Array in ascending order is : "); for (int str : arr) { System.out.println(str); } } }
So, in the above code we have created an Integer Array and initialised to the variable arr.
int[] arr = {5, 3, 2, 4};
Below is how the values are positioned in the Array,
Then we have used the sort() Method from the java.util package to sort the Array arr in increasing order.
Arrays.sort(arr);
And the numbers in the Array arr gets sorted.
And we get the below output.