-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSortingStringArrayElements.java
40 lines (33 loc) · 1.25 KB
/
SortingStringArrayElements.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
package interviewprograms;
import java.util.Arrays;
import java.util.Collections;
/*
*/
public class SortingStringArrayElements {
public static void main(String[] args) {
String []characters={"a","y", "q", "d", "g"};
System.out.println(" Elements: ");
for(String s: characters){
System.out.print(s);
}
Arrays.sort(characters);
System.out.println("\n Sorted in Ascending order Elements: "); //Descending order is printed
for(String s: characters){
System.out.print(s);
}
Arrays.sort(characters, Collections.reverseOrder()); //The reverseOrder() method is used to get a comparator that imposes the reverse of the natural ordering
System.out.println("\n Sorted in Descending order Elements: ");// on a collection of objects that implement the Comparable interface.
for(String s: characters){
System.out.print(s); //Descending order is printed
}
}
}
/*
run:
Elements:
ayqdg
Sorted in Ascending order Elements:
adgqy
Sorted in Descending order Elements:
yqgda
*/