1Here’s the java program to sort an array using Arrays.sort() method.
2
3import java.util.Arrays;
4public class JavaArraySortMethod
5{
6 public static void main(String[] args)
7 {
8 String[] strGiven = {"Great Barrier Reef", "Paris", "borabora", "Florence","tokyo", "Cusco"};
9 Arrays.sort(strGiven);
10 System.out.println("Output(case sensitive) : " + Arrays.toString(strGiven));
11 }
12}
1int countOfArray = 5;
2int num [] = new int[counfOfArray];
3
4int num[] = [8 , 5 , 9 , 3 , 4];
5
6for (int i = 0; i < countOfArray; i++)
7 {
8 for (int j = i + 1; j < countOfArray; j++) {
9 if (num[i] > num[j])
10 {
11 temp = num[i];
12 num[i] = num[j];
13 num[j] = temp;
14 }
15 }
16 }
1// an array of ints
2int[] arr = {1, 2, 3, 4, 5, 6};
3
4// an array of reverse sorted ints
5int[] arrDesc = Arrays.stream(arr).boxed()
6 .sorted(Collections.reverseOrder())
7 .mapToInt(Integer::intValue)
8 .toArray();
9
10System.out.println(Arrays.toString(arrDesc)); // outputs [6, 5, 4, 3, 2, 1]
1import java.util.Scanner;
2public class JavaExample
3{
4 public static void main(String[] args)
5 {
6 int count, temp;
7
8 //User inputs the array size
9 Scanner scan = new Scanner(System.in);
10 System.out.print("Enter number of elements you want in the array: ");
11 count = scan.nextInt();
12
13 int num[] = new int[count];
14 System.out.println("Enter array elements:");
15 for (int i = 0; i < count; i++)
16 {
17 num[i] = scan.nextInt();
18 }
19 scan.close();
20 for (int i = 0; i < count; i++)
21 {
22 for (int j = i + 1; j < count; j++) {
23 if (num[i] > num[j])
24 {
25 temp = num[i];
26 num[i] = num[j];
27 num[j] = temp;
28 }
29 }
30 }
31 System.out.print("Array Elements in Ascending Order: ");
32 for (int i = 0; i < count - 1; i++)
33 {
34 System.out.print(num[i] + ", ");
35 }
36 System.out.print(num[count - 1]);
37 }
38}