-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortByCFramework.java
More file actions
155 lines (94 loc) · 2.7 KB
/
Copy pathSortByCFramework.java
File metadata and controls
155 lines (94 loc) · 2.7 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.*;
class Student
{
int RegNum;
String NameOfStud;
String ClgName;
int Mark;
String Dept;
public Student(int RegNum,
String NameOfStud,
String ClgName,
int Mark,
String Dept)
{
this.RegNum=RegNum;
this.NameOfStud=NameOfStud;
this.ClgName=ClgName;
this.Mark=Mark;
this.Dept=Dept;
}
public String toString()
{
return this.RegNum+" "+this.NameOfStud
+" "+this.ClgName+" " +this.Mark +" "+this.Dept;
}
}
class SortByRegNum implements Comparator<Student>
{
public int compare(Student a, Student b)
{
return a.RegNum - b.RegNum;
}
}
class SortByRegName implements Comparator<Student>
{
public int compare(Student a, Student b)
{
return a.NameOfStud.compareTo(b.NameOfStud);
}
}
class SortByClgName implements Comparator<Student>
{
public int compare(Student a,Student b)
{
return a.ClgName.compareTo(b.ClgName);
}
}
class SortByMarks implements Comparator<Student>
{
public int compare(Student a,Student b)
{
return a.Mark - b.Mark;
}
}
class SortByDept implements Comparator<Student>
{
public int compare(Student a,Student b)
{
return a.Dept.compareTo(b.Dept);
}
}
class SortByCFramework
{
public static void main (String[] args)
{
ArrayList<Student> al = new ArrayList<Student>();
al.add(new Student(01, "KamalHassan","SRC,SASTRA university",99,"CSE"));
al.add(new Student(07, "MSDhoni","SASTRA university",98,"ECE"));
al.add(new Student(06, "Rajinikanth","Anna university",100,"EEE"));
System.out.println("Unsorted array list");
for (int i=0; i<al.size(); i++)
System.out.println(al.get(i));
Collections.sort(al, new SortByRegNum());
System.out.println("\nSorted by registerno");
for (int i=0; i<al.size(); i++)
System.out.println(al.get(i));
Collections.sort(al, new SortByRegName());
System.out.println("\nSorted by name of the students");
for (int i=0; i<al.size(); i++)
System.out.println(al.get(i));
Collections.sort(al, new SortByClgName());
System.out.println("\nSorted by ClgName");
for (int i=0; i<al.size(); i++)
System.out.println(al.get(i));
Collections.sort(al, new SortByMarks());
System.out.println("\nSorted by Marks");
for (int i=0; i<al.size(); i++)
System.out.println(al.get(i));
Collections.sort(al, new SortByDept());
System.out.println("\nSorted by Dept");
for (int i=0; i<al.size(); i++)
System.out.println(al.get(i));
}
}