From 61b8ae0b0967c4f01552ffede8020a131c751dc4 Mon Sep 17 00:00:00 2001 From: Simon Halvdansson Date: Mon, 2 Oct 2023 09:54:04 +0200 Subject: [PATCH] API 23 sorting backport --- .../utils/CommentSorter.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/simon/harmonichackernews/utils/CommentSorter.java b/app/src/main/java/com/simon/harmonichackernews/utils/CommentSorter.java index 7d3f583d..cc97538e 100644 --- a/app/src/main/java/com/simon/harmonichackernews/utils/CommentSorter.java +++ b/app/src/main/java/com/simon/harmonichackernews/utils/CommentSorter.java @@ -5,6 +5,7 @@ import com.simon.harmonichackernews.data.Comment; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -17,13 +18,28 @@ public static void sort(Context ctx, List comments) { for (int i = 1; i < comments.size(); i++) { comments.get(i).totalReplies = numChildren(comments, i); } - sortComments(comments, Comparator.comparingInt(c -> -c.totalReplies)); + sortComments(comments, new Comparator() { + @Override + public int compare(Comment c1, Comment c2) { + return Integer.compare(-c1.totalReplies, -c2.totalReplies); + } + }); break; case "Newest first": - sortComments(comments, Comparator.comparingInt(c -> -c.time)); + sortComments(comments, new Comparator() { + @Override + public int compare(Comment c1, Comment c2) { + return Integer.compare(-c1.time, -c2.time); + } + }); break; case "Oldest first": - sortComments(comments, Comparator.comparingInt(c -> c.time)); + sortComments(comments, new Comparator() { + @Override + public int compare(Comment c1, Comment c2) { + return Integer.compare(c1.time, c2.time); + } + }); break; } } @@ -42,7 +58,12 @@ private static void sortComments(List comments, Comparator com comments.get(0).sortOrder = -1; // Sort according to sortOrder from flattenCommentsWithChildren step - from sortOrder field - comments.sort(Comparator.comparingInt(e -> e.sortOrder)); + Collections.sort(comments, new Comparator() { + @Override + public int compare(Comment e1, Comment e2) { + return Integer.compare(e1.sortOrder, e2.sortOrder); + } + }); } private static void sortCommentsRecursive(List commentsWithChildren, Comparator comparator) {