nodechan

decentralized peer-to-peer anonymous messageboard client
git clone git://squid-tech.com/nodechan.git
Log | Files | Refs | README

commit 5e46a87fcc168bc7f7e594cde266567b2df541a7
parent 84b41f93c02c154d9a780ebf05e08b4ef61a9c60
Author: joshiemoore <jxm5210@g.rit.edu>
Date:   Thu,  8 Aug 2019 04:07:41 -0400

Sort threads in threadlist based on latest post time

Diffstat:
Msrc/ChanThread.java | 23+++++++++++++++++++++++
Msrc/NodeChan.java | 10++++++++++
2 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/src/ChanThread.java b/src/ChanThread.java @@ -19,6 +19,9 @@ public class ChanThread { /** The title of this thread **/ private String title; + /** The time of the most recent post in this thread **/ + private long lastTime; + /** Storage for all of the posts that have been made in this thread **/ private ArrayList<ChanPost> posts; @@ -39,6 +42,8 @@ public class ChanThread { this.posts = new ArrayList<ChanPost>(); this.title = ""; + + this.lastTime = 0; } /** @@ -62,6 +67,20 @@ public class ChanThread { this.posts.add(post); } + + this.lastTime = post.getPostTime(); + } + + /** + * For sorting purposes + * Sort from most recent (so, highest) post time to oldest (top-to-bottom) + */ + public int compareTo(ChanThread other) { + long t = this.lastTime - other.getLastTime(); + + if (t < 0) return 1; + else if (t > 0) return -1; + else return 0; } @@ -80,6 +99,10 @@ public class ChanThread { return this.posts.size(); } + public long getLastTime() { + return this.lastTime; + } + /** * We want to avoid giving outside classes direct access to the posts * ArrayList diff --git a/src/NodeChan.java b/src/NodeChan.java @@ -1,5 +1,7 @@ import java.util.Scanner; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -172,6 +174,14 @@ public class NodeChan { continue; } + // Sort the threads based on their last post time (most recent first) + Collections.sort(threads, new Comparator<ChanThread>() { + @Override + public int compare(ChanThread thread1, ChanThread thread2) { + return thread1.compareTo(thread2); + } + }); + System.out.println("TID Subject"); for (int i = 0; i < threads.size(); i++) { ChanThread l = threads.get(i);