此代码的结果是将mergedGrades移动到基于按等级对其进行排序的sortedGrades,然后是名称。我设法找到从最高到最低的成绩,但我无法按名称对成绩进行排序。请帮忙。
我的代码:
public static Grades sort(Grades mergedGrades, Grades repeat) {
Grades sortedGrades = new Grades();
while( (mergedGrades.size() > 0)){
Grade highComp = null;
Grade highest = mergedGrades.get(0);
int index = 0;
for (int j = 0; j < mergedGrades.size(); j++) {
highComp = mergedGrades.get(j);
if (highComp.getGpa() > highest.getGpa()) {
highest = highComp;
}
}
//sortedGrades.add(highest);
for(int i = 0 ; i < mergedGrades.size(); i++){
for (int i = 0; i < repeat.size(); i++) {
if (repeat.contains(highest)) {
if(highest.getFirstName().compareTo(repeat.get(i).getFirstName()) < 0) {
if(mergedGrades.get(i).getLastName().compareTo(highest.getLastName()) < 0) {
sortedGrades.add(highest);
System.out.println("test");
}
}
}
}
sortedGrades.add(highest);
mergedGrades.remove(index);
}
结果应如下所示:
S005: Stacy, Lu 4.0 S004: Aseef, Hernandez 3.9 S006: Aseef, Nilkund 3.9 S002: Jim, NLN 3.9 S003: Misty, Fang 3.9 S009: Steve, Calderon 3.9 S016: Aseef, Simmons 3.9 S010: Raj, Singh 3.8 S018: Hamza, Nilkund 3.5 S012: Kathy, Calderon 3.5 S017: Hifza, Nilkund 3.3 S011: Jason, Kramer 3.3 S001: John, Rodgers 3.3 S019: Chris, Peach 3.2 S013: Roopa, Singh 3.2 S020: Ramona, Luke 2.4 S014: Amid, Naveed 2.4 S015: Faith, Williams 1.0
答案 0 :(得分:0)
比较hashCode()
值不是对名称进行排序的方法。请改用compareTo()
:
if (mergedGrades.get(i).getFirstName().compareTo(
highest.getFirstName()) < 0) {
// mergedGrades firstname < highest firstname
}