From: David ‘Bombe’ Roden Date: Mon, 30 Jul 2012 08:56:18 +0000 (+0200) Subject: Add comparator that sorts by parties by name and year. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=01aeae3f0e571924efcdc5156497a78874b4bdeb;p=demoscenemusic.git Add comparator that sorts by parties by name and year. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultParty.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultParty.java index 1b3ace4..f621d4d 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultParty.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultParty.java @@ -18,6 +18,10 @@ package net.pterodactylus.demoscenemusic.data; import java.util.Collection; +import java.util.Comparator; + +import net.pterodactylus.util.number.Numbers; + /** * Default implementation of a party data container. * @@ -25,6 +29,23 @@ import java.util.Collection; */ public class DefaultParty extends DefaultBase implements Party { + /** Comparator that sorts parties by name and descending year. */ + public static final Comparator NAME_YEAR_COMPARATOR = new Comparator() { + + @Override + public int compare(Party leftParty, Party rightParty) { + int diff = leftParty.getName().compareToIgnoreCase(rightParty.getName()); + if (diff != 0) { + return diff; + } + int leftYear = Numbers.safeParseInteger(leftParty.getProperties().get("party/year"), 0); + int rightYear = Numbers.safeParseInteger(rightParty.getProperties().get("party/year"), 0); + /* show newest parties first. */ + return rightYear - leftYear; + } + + }; + /** * Creates a new party data container. *