From b99b66b87b12db4bf725996547c482efc7d7a08f Mon Sep 17 00:00:00 2001 From: cugurlu Date: Mon, 8 Apr 2019 14:34:38 +0200 Subject: [PATCH] Fix sorting errors --- .../client/controller/DashBoardController.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java index 5679911..33c80ea 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -253,24 +253,23 @@ public class DashBoardController { * Sorts the scores of users. * @param users the list of users */ - public List sortScores(List users) throws InterruptedException { + public void sortScores(List users) throws InterruptedException { for (int i = 0; i < users.size(); i++) { for (int j = 0; j < users.size(); j++) { Double first = userService.getFootprint(users.get(i)); Double second = userService.getFootprint(users.get(j)); - if (i < j && first > second) { + if (i < j && (first.compareTo(second) < 0)) { String temp = users.get(i); users.set(i, users.get(j)); users.set(j, temp); } - if (i > j && first < second) { + if (i > j && (first.compareTo(second) > 0)) { String temp = users.get(i); users.set(i, users.get(j)); users.set(j, temp); } } } - return users; } /** @@ -520,16 +519,16 @@ public class DashBoardController { //global leaderboard globalLeaderboard.getItems().clear(); globalLeaderData.removeAll(); - List userList = userService.getAllUsers(); - List firstList = sortScores(userList); //development leaderboard developmentLeaderboard.getItems().clear(); developmentData.removeAll(); - List secondList = sortDiffScores(userList); + List userList = userService.getAllUsers(); + sortScores(userList); for (int i = userList.size() - 1; i >= 0; i--) { - Friend user = new Friend(firstList.get(i), userService.getFootprint(firstList.get(i))); + Friend user = new Friend(userList.get(i), userService.getFootprint(userList.get(i))); globalLeaderData.add(user); } + List secondList = sortDiffScores(userList); for (int j = 0; j < userList.size(); j++) { double diff = Math.round((userService.getFirstFootprint(secondList.get(j)) - userService.getFootprint(secondList.get(j))) * 10) / 10.0; @@ -556,7 +555,7 @@ public class DashBoardController { friendLeaderboard.getItems().clear(); friendLeaderData.removeAll(); sortDiffScores(wholeList); - for (int i = 0; i < friendList.size(); i++) { + for (int i = friendList.size() - 1; i >= 0 ; i--) { Friend user = new Friend(friendList.get(i), userService .getFootprint(friendList.get(i))); data.add(user);