Skip to content

Commit

Permalink
API 23 sorting backport
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHalvdansson committed Oct 2, 2023
1 parent 9d126fb commit 61b8ae0
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,13 +18,28 @@ public static void sort(Context ctx, List<Comment> 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<Comment>() {
@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<Comment>() {
@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<Comment>() {
@Override
public int compare(Comment c1, Comment c2) {
return Integer.compare(c1.time, c2.time);
}
});
break;
}
}
Expand All @@ -42,7 +58,12 @@ private static void sortComments(List<Comment> comments, Comparator<Comment> 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<Comment>() {
@Override
public int compare(Comment e1, Comment e2) {
return Integer.compare(e1.sortOrder, e2.sortOrder);
}
});
}

private static void sortCommentsRecursive(List<Comment> commentsWithChildren, Comparator<Comment> comparator) {
Expand Down

0 comments on commit 61b8ae0

Please sign in to comment.