-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparableTest.java
More file actions
68 lines (54 loc) · 1.72 KB
/
ComparableTest.java
File metadata and controls
68 lines (54 loc) · 1.72 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
59
60
61
62
63
64
65
66
67
68
package com.interview.all;
import java.util.Collections;
import java.util.LinkedList;
public class ComparableTest implements Comparable<ComparableTest> {
private Integer id;
private String name;
private Integer rollno;
public ComparableTest( String name, Integer id, Integer rollno) {
super();
this.id = id;
this.name = name;
this.rollno = rollno;
}
/*
@Override
public int compareTo(ComparableTest c) {
if(id>c.id) {
return 1; //descending order
}else if(id==c.id) {
return 0;
}else {
return -1;
}
}
*/
@Override
public int compareTo(ComparableTest c) {
return this.name.compareTo(c.name);
}
public static void main(String[] args) {
// Create one LinkedList for Student object
LinkedList<ComparableTest> list
= new LinkedList<ComparableTest>();
list.add(new ComparableTest("Meet", 32, 2));
list.add(new ComparableTest("Jhon", 11, 5));
list.add(new ComparableTest("Sham", 92, 1));
list.add(new ComparableTest("William", 86, 3));
list.add(new ComparableTest("Harry", 35, 4));
System.out.println("before sorting-------------------------");
for (ComparableTest s : list) {
System.out.println(s.name + " " + s.id + " "+ s.rollno);
}
System.out.println("after sorting---------------------------");
Collections.sort(list);
for (ComparableTest s : list) {
System.out.println(s.name + " " + s.id + " "+ s.rollno);
}
Collections.sort(list,Collections.reverseOrder());
System.out.println("sorting in revers order-----------------");
for (ComparableTest s : list) {
System.out.println(s.name + " " + s.id + " "+ s.rollno);
}
}
}