From 286f10b9dfd86ebb336ffc18b76935d32c943158 Mon Sep 17 00:00:00 2001 From: bowser0000 Date: Thu, 23 Jul 2020 17:13:07 -0400 Subject: Sort pets by xp --- me/Danker/commands/PetsCommand.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'me/Danker/commands') diff --git a/me/Danker/commands/PetsCommand.java b/me/Danker/commands/PetsCommand.java index c9c1af7..1ffdd77 100644 --- a/me/Danker/commands/PetsCommand.java +++ b/me/Danker/commands/PetsCommand.java @@ -1,6 +1,7 @@ package me.Danker.commands; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import com.google.gson.JsonArray; @@ -116,14 +117,27 @@ public class PetsCommand extends CommandBase { } System.out.println("Looping through pets..."); + // Push each pet into list + List sortedPets = new ArrayList(); + for (JsonElement petElement : petsArray) { + sortedPets.add(petElement); + } + // Sort pets by exp + Collections.sort(sortedPets, (pet1, pet2) -> { + double petXp1 = pet1.getAsJsonObject().get("exp").getAsDouble(); + double petXp2 = pet2.getAsJsonObject().get("exp").getAsDouble(); + return -Double.compare(petXp1, petXp2); + }); + + // Sort pets into rarities List commonPets = new ArrayList(); List uncommonPets = new ArrayList(); List rarePets = new ArrayList(); List epicPets = new ArrayList(); List legendaryPets = new ArrayList(); - for (JsonElement petElement : petsArray) { + for (JsonElement petElement : sortedPets) { JsonObject pet = petElement.getAsJsonObject(); String rarity = pet.get("tier").getAsString(); -- cgit