From 1ed56f10da3ddf2a84a30c70df90dbe454cd745f Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 4 Oct 2022 21:26:05 +0200 Subject: [PATCH] Add logic for sorting competitors --- .../ch/nuth/zhaw/exbox/RankingListServer.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/RankingListServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/RankingListServer.java index da17614..9801b24 100644 --- a/app/src/main/java/ch/nuth/zhaw/exbox/RankingListServer.java +++ b/app/src/main/java/ch/nuth/zhaw/exbox/RankingListServer.java @@ -39,8 +39,24 @@ public class RankingListServer implements CommandExecutor { } public String createSortedText(List competitorList) { - // TODO Implement - return ""; + List competitors = competitorList.stream().sorted().toList(); + String previousTime = null; + int previousRank = 1; + + for (int i = 0; i < competitors.size(); i++) { + Competitor competitor = competitors.get(i); + + if (competitor.getTime() != previousTime) { + previousRank = i + 1; + previousTime = competitor.getTime(); + } + + competitor.setRank(previousRank); + } + + return String.join( + System.lineSeparator(), + competitors.stream().map((competitor) -> competitor.toString()).toList()); } public String createNameList(List competitorList) {