Skip to content

Instantly share code, notes, and snippets.

@orb orb/CommentFun.java
Created Jul 2, 2015

Embed
What would you like to do?
Java 7 sorting.
package hello;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class CommentFun
{
public static void main(String[] args)
{
Comment c1 = new Comment(1, "joe", "hello", "my comment 1");
Comment c2 = new Comment(2, "bob", "hello", "my comment 2");
Comment c3 = new Comment(3, "bill", "hello", "my comment 3");
Comment c4 = new Comment(4, "mary", "hello", "my comment 3");
List<Comment> comments = new ArrayList<>();
comments.add(c2);
comments.add(c3);
comments.add(c1);
comments.add(c4);
comments.sort(new Comparator<Comment>() {
public int compareIds(Integer id1, Integer id2) {
return id1.compareTo(id2);
}
@Override
public int compare(Comment c1, Comment c2) {
//return c2.getAuthor().compareTo(c1.getAuthor());
return compareIds(c2.getId(), c1.getId());
}
});
for (Comment comment: comments) {
System.out.println("*"+ comment.getId() + ": " + comment.getAuthor());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.