From 40e34e799991d534f2892f65ef454b5dd2d5bbd7 Mon Sep 17 00:00:00 2001 From: BONNe Date: Wed, 4 Sep 2019 09:30:11 +0300 Subject: [PATCH] Add better challenge sorting in AdminGUI's Now challenges will be sorted by their level order number, their order number, their unique id. All free challenges will always be at the start. --- .../challenges/ChallengesManager.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/main/java/world/bentobox/challenges/ChallengesManager.java b/src/main/java/world/bentobox/challenges/ChallengesManager.java index f2b65e0..2ad14ba 100644 --- a/src/main/java/world/bentobox/challenges/ChallengesManager.java +++ b/src/main/java/world/bentobox/challenges/ChallengesManager.java @@ -94,6 +94,45 @@ public class ChallengesManager public static final String FREE = ""; + // --------------------------------------------------------------------- + // Section: Comparators + // --------------------------------------------------------------------- + + + /** + * This comparator orders challenges by their level, order and name. + */ + private final Comparator challengeComparator = (o1, o2) -> { + if (o1.getLevel().equals(o2.getLevel())) + { + if (o1.getOrder() == o2.getOrder()) + { + // If orders are equal, sort by unique Id + return o1.getUniqueId().compareToIgnoreCase(o2.getUniqueId()); + } + else + { + // If levels are equal, sort them by order numbers. + return Integer.compare(o1.getOrder(), o2.getOrder()); + } + } + else + { + if (o1.getLevel().isEmpty() || o2.getLevel().isEmpty()) + { + // If exist free level challenge, then it should be at the start. + return Boolean.compare(o2.getLevel().isEmpty(), o1.getLevel().isEmpty()); + } + else + { + // Sort by challenges level order numbers + return Integer.compare(this.getLevel(o1.getLevel()).getOrder(), + this.getLevel(o2.getLevel()).getOrder()); + } + } + }; + + // --------------------------------------------------------------------- // Section: Constructor // --------------------------------------------------------------------- @@ -1385,7 +1424,7 @@ public class ChallengesManager { return this.islandWorldManager.getAddon(world).map(gameMode -> this.challengeCacheData.values().stream(). - sorted(Comparator.comparing(Challenge::getOrder)). + sorted(this.challengeComparator). filter(challenge -> challenge.matchGameMode(gameMode.getDescription().getName())). map(Challenge::getUniqueId). collect(Collectors.toList())). @@ -1403,7 +1442,7 @@ public class ChallengesManager { return this.islandWorldManager.getAddon(world).map(gameMode -> this.challengeCacheData.values().stream(). - sorted(Comparator.comparing(Challenge::getOrder)). + sorted(this.challengeComparator). filter(challenge -> challenge.matchGameMode(gameMode.getDescription().getName())). collect(Collectors.toList())). orElse(Collections.emptyList());