-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListWithComparator.java
51 lines (39 loc) · 1.26 KB
/
ListWithComparator.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
package listwithcomparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ListWithComparator {
public static void main(String[] args) {
// TODO code application logic here
List<Integer> list=new ArrayList<Integer>();
list.add(101);
list.add(109);
list.add(102);
list.add(108);
System.out.println("Before sorting:");
for(int i: list){
System.out.println(i);
}
//sorting the list by using sort method
Collections.sort(list);
System.out.println("After sorting:");
for(int i : list){
System.out.println(i);
}
Comparator<Integer> com=new ComClass();
Collections.sort(list,com);
System.out.println("After sorting with the last no:");
for(int i : list){
System.out.println(i);
}
}
public static class ComClass implements Comparator<Integer>{
@Override
public int compare(Integer t, Integer t1) {
if(t%10>t1%10)
return 0;
return -1;
}
}
}