Skip to content

Instantly share code, notes, and snippets.

@yaswanthrajyadiki
Created September 1, 2015 10:44
Show Gist options
  • Save yaswanthrajyadiki/f0618938e2f9810165b2 to your computer and use it in GitHub Desktop.
Save yaswanthrajyadiki/f0618938e2f9810165b2 to your computer and use it in GitHub Desktop.
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Collections;
import java.util.TreeSet;
class SortGrade {
List<Student> stuList;
SortGrade(List<Student> stuList) {
this.stuList = stuList;
}
public int compareDouble(double val1, double val2) {
if (val1 > val2) return 1;
if (val1 < val2) return -1;
return 0;
}
public int compareStrings(String str1, String str2) {
if (str1.compareTo(str2) > 0) return 1;
if (str1.compareTo(str2) < 0) return -1;
return 0;
}
public List<Student> sortingGrade() {
for (int i = 0; i < stuList.size(); i++) {
for (int j = 0; j < stuList.size(); j++) {
if (this.compareDouble(stuList.get(i).getAvgGrade(),
stuList.get(j).getAvgGrade()) == 1) {
Collections.swap(stuList, i, j);
} else if (this.compareDouble(stuList.get(i).getAvgGrade(),
stuList.get(j).getAvgGrade()) == 0) {
if (this .compareDouble(stuList.get(i).getGradeInMaths(),
stuList.get(j).getGradeInMaths()) == 1 ) {
Collections.swap(stuList,i,j);
} else if (this .compareDouble(stuList.get(i).getGradeInMaths(),
stuList.get(j).getGradeInMaths()) == 0) {
if (this .compareDouble(stuList.get(i).getGradeInScience(),
stuList.get(j).getGradeInScience()) == 1) {
Collections.swap(stuList,i,j);
} else if (this .compareDouble(stuList.get(i).getGradeInScience(),
stuList.get(j).getGradeInScience()) == 0) {
if (this .compareDouble(stuList.get(i).getGradeInSocial(),
stuList.get(j).getGradeInSocial()) == 1) {
Collections.swap(stuList,i,j);
} else if (this .compareDouble(stuList.get(i).getGradeInSocial(),
stuList.get(j).getGradeInSocial()) == 0) {
if (this .compareDouble(stuList.get(i).getGradeInFirstLang(),
stuList.get(j).getGradeInFirstLang()) == 1) {
Collections.swap(stuList,i,j);
} else if(this .compareDouble(stuList.get(i).getGradeInFirstLang(),
stuList.get(j).getGradeInFirstLang()) == 0){
if (this .compareStrings(stuList.get(i).getDateOfBirth(),
stuList.get(j).getDateOfBirth()) == -1) {
Collections.swap(stuList,i,j);
} else if (this .compareStrings(stuList.get(i).getDateOfBirth(),
stuList.get(j).getDateOfBirth()) == 0) {
if (this .compareStrings(stuList.get(i).getHallTicketNo(),
stuList.get(j).getHallTicketNo()) == -1) {
Collections.swap(stuList,i,j);
}
}
}
}
}
}
}
}
}
return stuList;
}
}
import java.util.List;
import java.util.ArrayList;
class SortGradeDemo {
public static void main(String[] args) {
List<Student> stuList = new ArrayList<Student>();
Student s1 = new Student(8.5, 7.6, 7.3, 8.0, 7.9, "21/08/1994", "IH110");
Student s2 = new Student(7.6, 8.6, 6.3, 7.5, 7.2, "2/01/1993", "IH113");
Student s3 = new Student(7.4, 7.5, 7.6, 8.0, 8.5, "12/06/1994", "IH080");
Student s4 = new Student(8.2, 9.2, 7.2, 7.0, 8.2, "14/11/1992", "IH085");
Student s5 = new Student(8.5, 7.6, 7.3, 8.0, 7.9, "21/08/1994", "IH007");
stuList.add(s1);
stuList.add(s2);
stuList.add(s3);
stuList.add(s4);
stuList.add(s5);
SortGrade sg = new SortGrade(stuList);
stuList = sg.sortingGrade();
for (Student stu : stuList) {
System.out.println(stu);
}
}
}
class Student {
double gradeInMaths;
double gradeInScience;
double gradeInEnglish;
double gradeInSocial;
double gradeInFirstLang;
String dateOfBirth;
String hallTicketNo;
double avgGrade;
Student(double gradeInMaths, double gradeInScience, double gradeInEnglish,
double gradeInSocial, double gradeInFirstLang,String dateOfBirth,
String hallTicketNo) {
this.gradeInMaths = gradeInMaths;
this.gradeInScience = gradeInScience;
this.gradeInEnglish = gradeInEnglish;
this.gradeInSocial = gradeInSocial;
this.gradeInFirstLang = gradeInFirstLang;
this.dateOfBirth = dateOfBirth;
this.hallTicketNo = hallTicketNo;
this.avgGrade = (gradeInMaths + gradeInScience + gradeInEnglish +
gradeInSocial + gradeInFirstLang)/5;
}
public double getAvgGrade() {
return this.avgGrade;
}
public double getGradeInMaths() {
return this.gradeInMaths;
}
public double getGradeInScience() {
return this.gradeInScience;
}
public double getGradeInSocial() {
return this.gradeInSocial;
}
public double getGradeInEnglish() {
return this.gradeInEnglish;
}
public double getGradeInFirstLang() {
return this.gradeInFirstLang;
}
public String getDateOfBirth() {
return this.dateOfBirth;
}
public String getHallTicketNo() {
return this.hallTicketNo;
}
public String toString() {
String s = "Maths: " + gradeInMaths + "\t";
s = s + "Science: " + gradeInScience + "\t";
s = s + "English: " + gradeInEnglish + "\t";
s = s + "Social: " + gradeInSocial + "\t";
s = s + "FirstLang: " + gradeInFirstLang + "\t";
s = s + "AvgGrade: " + avgGrade + "\t";
s = s + "DateOfBirth: " + dateOfBirth + "\t";
s = s + "HallTicketNo: " + hallTicketNo + ".";
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment