-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestSort4.java
More file actions
38 lines (38 loc) · 813 Bytes
/
testSort4.java
File metadata and controls
38 lines (38 loc) · 813 Bytes
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
import java.util.*;
class student
{
String name;
int rno,pmarks;
student(int rno,String name,int pmarks)
{
this.name=name;
this.rno=rno;
this.pmarks=pmarks;
}
}
class CompareByName implements Comparator
{
public int compare(Object s1,Object s2)//This has to be Object coz original declaration has type Object as params
{
student s3=(student)s1;
student s4=(student)s2;
return s3.name.compareTo(s4.name);
}
}
class TestSort4
{
public static void main(String... g)
{
ArrayList<student> al=new ArrayList<student>();
al.add(new student(101,"Vijay",23));
al.add(new student(106,"Ajay",27));
al.add(new student(105,"Jai",21));
Collections.sort(al,new CompareByName());
Iterator<student> itr=al.iterator();
while(itr.hasNext())
{
student st=itr.next();
System.out.println(st.name+"\t"+st.rno+"\t"+st.pmarks);
}
}
}