Example 1: sort array java
Here’s the java program to sort an array using Arrays.sort() method.
import java.util.Arrays;
public class JavaArraySortMethod
{
public static void main(String[] args)
{
String[] strGiven = {"Great Barrier Reef", "Paris", "borabora", "Florence","tokyo", "Cusco"};
Arrays.sort(strGiven);
System.out.println("Output(case sensitive) : " + Arrays.toString(strGiven));
}
}
Example 2: how the sort function works javascript
const unsorted = ['d', 'd', 'h', 'r', 'v', 'z', 'f', 'c', 'g'];
const sorted = unsorted.sort();
console.log(sorted);
//["c", "d", "d", "f", "g", "h", "r", "v", "z"]
const unsortedNums = [45, 56, 3, 3, 4, 6, 7, 45, 1];
const sortedNums = unsortedNums.sort((a, b) => {
return a - b;
});
console.log(sortedNums);
//[1, 3, 3, 4, 6, 7, 45, 45, 56]
Example 3: java sort method
Arrays.sort();//this method is an inbuild function in java
//to sort the array
Example 4: how to sort array
array sorting
Example 5: how to sort array
public static void main(String args[])
{
int [] array = new int[10];
array[0] = ((int)(Math.random()*100+1));
array[1] = ((int)(Math.random()*100+1));
array[2] = ((int)(Math.random()*100+1));
array[3] = ((int)(Math.random()*100+1));
array[4] = ((int)(Math.random()*100+1));
array[5] = ((int)(Math.random()*100+1));
array[6] = ((int)(Math.random()*100+1));
array[7] = ((int)(Math.random()*100+1));
array[8] = ((int)(Math.random()*100+1));
array[9] = ((int)(Math.random()*100+1));
System.out.println(array[0] +" " + array[1] +" " + array[2] +" " + array[3]
+" " + array[4] +" " + array[5]+" " + array[6]+" " + array[7]+" "
+ array[8]+" " + array[9] );
}