From: David ‘Bombe’ Roden <bombe@pterodactylus.net>
Date: Thu, 14 Oct 2010 13:46:15 +0000 (+0200)
Subject: Sort posts in Sones.
X-Git-Tag: 0.1-RC1~373
X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=5391ee6663db685d2e6283117ff8a17e91165350;p=Sone.git

Sort posts in Sones.
---

diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java
index 9df1a8f..3baed89 100644
--- a/src/main/java/net/pterodactylus/sone/data/Sone.java
+++ b/src/main/java/net/pterodactylus/sone/data/Sone.java
@@ -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;
 	}
 
 	/**
diff --git a/src/main/java/net/pterodactylus/sone/data/SoneShell.java b/src/main/java/net/pterodactylus/sone/data/SoneShell.java
index 8d8690a..16afea1 100644
--- a/src/main/java/net/pterodactylus/sone/data/SoneShell.java
+++ b/src/main/java/net/pterodactylus/sone/data/SoneShell.java
@@ -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;
 	}
 
 	/**