-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComparableExample.java
49 lines (38 loc) · 1.03 KB
/
ComparableExample.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 collections;
import java.util.*;
public class ComparableExample implements Comparable<ComparableExample> {
int rollno;
String name;
int age;
ComparableExample(int rollno,String name,int Age)
{
this.rollno=rollno;
this.name=name;
this.age=Age;
}
@Override
public String toString() {
return "ComparableExample [rollno=" + rollno + ", name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(ComparableExample a) {
// TODO Auto-generated method stub
if(age == a.age)
return 0;
if(age > a.age)
return 1;
return -1;
}
public static <E> void main(String args[]){
ArrayList<ComparableExample> s=new ArrayList<ComparableExample>();
s.add(new ComparableExample(123,"ramana",22));
s.add(new ComparableExample(345,"soniya",23));
s.add(new ComparableExample(567,"madhesh",22));
s.add(new ComparableExample(789,"raji",24));
Collections.sort(s);
for(ComparableExample a : s)
{
System.out.println(a);
}
}
}