solved s3 ex4

This commit is contained in:
navid.sassan 2020-10-11 18:02:10 +02:00
parent 626f0d1bee
commit 8c647448ea
2 changed files with 22 additions and 6 deletions

View File

@ -63,12 +63,12 @@ public class Competitor implements Comparable<Competitor> {
if (this.equals(other)) {
return 0;
}
return new Long(this.getTime()).compareTo(new Long(other.getTime()));
return Long.valueOf(this.getTime()).compareTo(Long.valueOf(other.getTime()));
}
public boolean equals(Competitor other) {
return this.getName().equals(other.getName())
&& new Integer(this.getJg()).equals(new Integer(other.getJg()))
&& new Long(this.getTime()).equals(new Long(other.getTime()));
&& Integer.valueOf(this.getJg()).equals(Integer.valueOf(other.getJg()))
&& Long.valueOf(this.getTime()).equals(Long.valueOf(other.getTime()));
}
}

View File

@ -1,10 +1,11 @@
package ch.zhaw.ads;
import java.util.ArrayList;
import java.util.List;
import java.text.ParseException;
import java.lang.ArrayIndexOutOfBoundsException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class RankingServer implements CommandExecutor {
List<Competitor> competitors;
@ -27,6 +28,21 @@ public class RankingServer implements CommandExecutor {
competitor.setRank(i + 1);
result.append(competitor.toString() + "\n");
}
Collections.sort(competitors, new Comparator<Competitor>() {
@Override
public int compare(Competitor o1, Competitor o2) {
int nameCompare = o1.getName().compareTo(o2.getName());
if (nameCompare == 0) {
return Integer.valueOf(o1.getJg()).compareTo(o2.getJg());
}
return nameCompare;
}
});
result.append("\n\nSorted Competitors (by name, then birth year):\n");
for (Competitor competitor : competitors) {
result.append(competitor.toString() + "\n");
}
return result.toString();
}