-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList_Sort
More file actions
115 lines (96 loc) · 3.01 KB
/
ArrayList_Sort
File metadata and controls
115 lines (96 loc) · 3.01 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
/* I used the Bubble sort */
/***********************************
You are given a list of student information: ID, FirstName, and CGPA.
Your task is to rearrange them according to their CGPA in decreasing order.
If two student have the same CGPA, then arrange them according to their first name in alphabetical order.
If those two students also have the same first name, then order them according to their ID.
No two students have the same ID.
Sample input file (file.txt)
6
33 Rumpa 3.68
85 Ashis 3.85
56 Samiha 3.75
19 Samara 3.75
22 Fahim 3.76
11 Samiha 3.75
Sample Output
Ashis 3.85 85
Fahim 3.76 22
Samara 3.75 19
Samiha 3.75 11
Samiha 3.75 56
Rumpa 3.68 33
************************************/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
class Student {
private int id;
private String fname;
private double cgpa;
public Student(int i, String fn, double cg) {
id = i;
fname = fn;
cgpa = cg;
}
public int getId() {
return id;
}
public String getFname() {
return fname;
}
public double getCgpa() {
return cgpa;
}
}
public class Solution
{
public static ArrayList<Student> swap (ArrayList<Student> l, int index1, int index2) {
Student tmp = l.get(index1);
l.set(index1, l.get(index2));
l.set(index2, tmp);
return l;
}
public static void main(String[] args) throws FileNotFoundException {
File f = new File("file.txt");
Scanner sc = new Scanner(f);
int testCases = sc.nextInt();
int size = testCases;
ArrayList<Student> l = new ArrayList<>();
while(testCases>0){
int id = sc.nextInt();
String fname = sc.next();
double cgpa = sc.nextDouble();
Student st = new Student(id, fname, cgpa);
l.add(st);
testCases--;
}
sc.close();
for (int i=0; i<size-1; i++) {
for (int j=0; j<size-1-i; j++) {
if (l.get(j).getCgpa()<l.get(j+1).getCgpa()) {
swap(l, j, j+1);
}
else if (l.get(j).getCgpa()==l.get(j+1).getCgpa()) {
if (l.get(j).getFname().compareTo(l.get(j+1).getFname())>0) {
swap(l, j, j+1);
}
else if (l.get(j).getFname().compareTo(l.get(j+1).getFname())==0) {
if (l.get(j).getId()>l.get(j+1).getId()) {
swap(l, j, j+1);
}
}
}
}
}
for(Student st: l){
//System.out.println(st.getFname()+" "+st.getCgpa()+" "+st.getId());
System.out.printf("%15s %10.2f %5d%n", st.getFname(), st.getCgpa(), st.getId());
}
}
}
/*****
A simple way
Collections.sort(l, Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getFname).thenComparing(Student::getId));
******/