Add comparator that sorts by parties by name and year.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 30 Jul 2012 08:56:18 +0000 (10:56 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 30 Jul 2012 08:56:18 +0000 (10:56 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DefaultParty.java

index 1b3ace4..f621d4d 100644 (file)
 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<Party> NAME_YEAR_COMPARATOR = new Comparator<Party>() {
+
+               @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.
         *