-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comparable.java
49 lines (42 loc) · 1.16 KB
/
Comparable.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
package JavaPrograms;
import java.util.*;
import java.util.Comparator;
import java.util.List;
class Students1 implements java.lang.Comparable<Students1>
{
String Name;
@Override
public String toString() {
return "Students1{" +
"Name=" + Name +
", roll_no=" + roll_no +
", marks=" + marks +
'}';
}
public Students1(String name, int roll_no, int marks) {
Name = name;
this.roll_no = roll_no;
this.marks = marks;
}
int roll_no;
int marks;
@Override
public int compareTo(Students1 o) {
return this.marks>o.marks?-1:this.marks<o.marks?1:0;
}
}
public class Comparable {
public static void main(String arg[])
{
List<Students1> stu = new ArrayList<>();
stu.add(new Students1("Jack", 1, 98));
stu.add(new Students1("Marie", 2, 78));
stu.add(new Students1("John", 3, 82));
Collections.sort(stu);
System.out.println("Records in sorted marks : ");
for(Students1 s : stu)
{
System.out.println(s);
}
}
}