In this post under Java, I will show with example how to sort an array.
Below is the complete main method for reference.
Main class
package core.collection;
import java.util.Arrays;
public class ArraysSortDemo {
public static void main(String[] args) {
int[] integerList = new int[] {5, 2, 3, 1, 4};
Arrays.sort(integerList);
for(int value : integerList) {
System.out.println(value);
}
}
}
In the above at line 7, I created a list of unsorted integers.
At line 8, I use the “Arrays” class static method “sort” to sort the array.
It will sort the elements of the given array in ascending order.
In this way we can sort an array.