Created
July 2, 2015 16:12
-
-
Save orb/c0829030b9f18a35ae17 to your computer and use it in GitHub Desktop.
Java 7 sorting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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