From 01aeae3f0e571924efcdc5156497a78874b4bdeb Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 30 Jul 2012 10:56:18 +0200 Subject: [PATCH] Add comparator that sorts by parties by name and year. --- .../demoscenemusic/data/DefaultParty.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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. * -- 2.7.4