From 5391ee6663db685d2e6283117ff8a17e91165350 Mon Sep 17 00:00:00 2001
From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?=
 <bombe@pterodactylus.net>
Date: Thu, 14 Oct 2010 15:46:15 +0200
Subject: [PATCH] Sort posts in Sones.

---
 src/main/java/net/pterodactylus/sone/data/Sone.java      | 14 ++++++++++++--
 src/main/java/net/pterodactylus/sone/data/SoneShell.java | 14 ++++++++++++--
 2 files changed, 24 insertions(+), 4 deletions(-)

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;
 	}
 
 	/**
-- 
2.7.4