Sorting The Array (Sort Method)
Well, as the name of the method says, it is going to sort our array in ascending order. So, whenever we need our array sorted, we can make use of this Array Sort Java Method. Let’s have a look at a sample program –
import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
int[] x = new int[] {56, 29, 31, 3, 45, 20, 21};
Arrays.sort(x);
System.out.println(Arrays.toString(x));
}
}
If we try to execute the above program, we can find that the array that we had given as input is now sorted in ascending order. Note that we have passed an array to that method, and the arrays are passed as reference, so the changes are directly made in the array itself (a fact that array objects are mutable). So, if you now print the array, it will print sorted. Let’s have a look at the program to demonstrate the sort method.
import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
int[] somearray = new int[] {12, 5, 8, 45, 29, 31};
Arrays.sort(somearray);
System.out.println(Arrays.toString(somearray));
}
}
So, as you can see now, the original array is sorted using the sort method. Many times we might need our array sorted, so in such situations, we can use the sort method. Well, this method is overloaded, which means that there are some other sort methods in the class Array, which have the same name as sort, but they receive different arguments. Like, we have a sort method for sorting a double array, a character array, a string array, etc. also, and we can sort the array within a specified range of indexes.
Have a look at the below programs. The first program is a demonstration to sort a double array using the sort method, the second one demonstrates the sorting of a character array, the third program demonstrates the sorting of a string array, and the fourth program demonstrates sorting for a specified range.
Program to demonstrate sorting of double array –
import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
double[] somearray = new double[]{12.034, 5.23, 8.12, 45, 29, 31};
Arrays.sort(somearray);
System.out.println(Arrays.toString(somearray));
}
}
As we execute this program, the double array prints out sorted. Now let’s have a look at sorting some character array –
import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
char[] somearray = new char[]{‘i’, ‘b’, ‘e’,’g’, ‘n’};
Arrays.sort(somearray);
System.out.println(Arrays.toString(somearray));
}
}
So, if we try to execute the above program, we find that the character array is also sorted. If you are wondering how are we sorting the character array, then it is according to their codes.
This is just like b is less than e, so b comes first. Now let’s see how can we sort a string array.
import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
String[] somearray = new String[]{“zxc”, “asd”, “qwe”};
Arrays.sort(somearray);
System.out.println(Arrays.toString(somearray));
}
}
So, in the above program, the string array is sorted. Here, we are comparing one string with another, character-wise. So, if we compare z(from array element 0) with a (from array element 1), we find that a is smaller than z, so the string asd becomes smaller than zxc, and we are following ascending order, so the asd comes first. This way, the array is sorted, and we are printing again the string representation of the array after sorting.
Now let’s have a look at the program which demonstrates us sorting within a specific range of indexes. This is like we don’t want to sort whole array, just from this index to that index. Let’s have a look at the implementation –
import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
int[] somearray = new int[]{56, 43, 29, 31, 14, 4, 55};
Arrays.sort(somearray, 2, 6);
System.out.println(Arrays.toString(somearray));
}
}
Well, we have now passed two more arguments to the sort method, which are from index and to index. This is like we are telling that sort some array from index 2 to index 6. This is important to note that here, the from the index is included but the index is not included. This means that if we have given fromIndex as 2, and toIndex as 6, we are sorting the elements from index 2 to 5. the toIndex is not included. Observe the output from the above program to understand in what range is the sorting being done.