Sort posts in Sones.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 14 Oct 2010 13:46:15 +0000 (15:46 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 14 Oct 2010 13:46:15 +0000 (15:46 +0200)
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/data/SoneShell.java

index 9df1a8f..3baed89 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.data;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -211,12 +212,21 @@ public class Sone {
        }
 
        /**
-        * Returns the list of posts of this Sone.
+        * Returns the list of posts of this Sone, sorted by time, newest first.
         *
         * @return All posts of this Sone
         */
        public List<Post> getPosts() {
-               return Collections.unmodifiableList(posts);
+               List<Post> sortedPosts = new ArrayList<Post>(posts);
+               Collections.sort(sortedPosts, new Comparator<Post>() {
+
+                       @Override
+                       public int compare(Post leftPost, Post rightPost) {
+                               return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
+                       }
+
+               });
+               return sortedPosts;
        }
 
        /**
index 8d8690a..16afea1 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.data;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -221,13 +222,22 @@ public class SoneShell extends Sone implements Shell<Sone> {
        }
 
        /**
-        * Returns the list of posts of this Sone.
+        * Returns the list of posts of this Sone, sorted by time, newest first.
         *
         * @return All posts of this Sone
         */
        @Override
        public List<Post> getPosts() {
-               return Collections.unmodifiableList(posts);
+               List<Post> sortedPosts = new ArrayList<Post>(posts);
+               Collections.sort(sortedPosts, new Comparator<Post>() {
+
+                       @Override
+                       public int compare(Post leftPost, Post rightPost) {
+                               return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
+                       }
+
+               });
+               return sortedPosts;
        }
 
        /**