-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCode Example of Sorting Array in Java.java
74 lines (61 loc) · 1.65 KB
/
Code Example of Sorting Array in Java.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Arrays;
import java.util.Collections;
public class HashtableDemo {
public static void main(String args[]) {
String[] companies = { "Google", "Apple", "Sony" };
// sorting java array in ascending order
System.out.println("Sorting String Array in Ascending order in Java Example");
System.out.println("Unsorted String Array in Java: ");
printNumbers(companies);
Arrays.sort(companies);
System.out.println("Sorted String Array in ascending order : ");
printNumbers(companies);
// sorting java array in descending order
System.out.println("Sorting Array in Descending order in Java Example");
System.out.println("Unsorted int Array in Java: ");
printNumbers(companies);
Arrays.sort(companies, Collections.reverseOrder());
System.out.println("Sorted int Array in descending order : ");
printNumbers(companies);
System.out.println("Sorting part of array in java:");
int[] numbers = { 1, 3, 2, 5, 4 };
Arrays.sort(numbers, 0, 3);
System.out.println("Sorted sub array in Java: ");
for (int num : numbers) {
System.out.println(num);
}
}
public static void printNumbers(String[] companies) {
for (String company : companies) {
System.out.println(company);
}
}
}
/*
Output:
Sorting String Array in Ascending order in Java Example
Unsorted String Array in Java:
Google
Apple
Sony
Sorted String Array in ascending order :
Apple
Google
Sony
Sorting Array in Descending order in Java Example
Unsorted int Array in Java:
Apple
Google
Sony
Sorted String Array in descending order :
Sony
Google
Apple
Sorting part of array in java:
Sorted sub array in Java:
1
2
3
5
4
Read more: http://javarevisited.blogspot.com/2012/01/sort-array-in-java-ascending-and.html#ixzz4rJ2A2HWy*/