-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArraysSortDemo.java
More file actions
58 lines (46 loc) · 1 KB
/
ArraysSortDemo.java
File metadata and controls
58 lines (46 loc) · 1 KB
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
import java.util.*;
class ArraysSortDemo
{
public static void main(String[] args)
{
int[] arr = {10, 5, 20, 11, 6};
System.out.println("Primitive Array Before Sorting:");
for(int val: arr)
{
System.out.println(val);
}
Arrays.sort(arr);
System.out.println("Primitive Array After Sorting:");
for(int val: arr)
{
System.out.println(val);
}
String[] str = {"A", "Z", "B"};
System.out.println("Object Array Before Sorting");
for(String str1: str)
{
System.out.println(str1);
}
Arrays.sort(str);
System.out.println("Object Array After Sorting");
for(String str1: str)
{
System.out.println(str1);
}
Arrays.sort(str, new MyComparator());
System.out.println("Object Array After Sorting by Comparator");
for(String str1: str)
{
System.out.println(str1);
}
}
}
class MyComparator implements Comparator
{
public int compare(Object obj1, Object obj2)
{
String str1 = obj1.toString();
String str2 = obj2.toString();
return str2.compareTo(str1);
}
}