Guest User

Untitled

a guest
Jul 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. Collection<customObject> list = new ArrayList<customObject>();
  2.  
  3. Collection list = new ArrayList();
  4. Comparator comparator = new new Comparator<CustomObject>() {
  5. public int compare(CustomObject c1, CustomObject c2) {
  6. return c2.getId() - c1.getId(); // use your logic
  7. }
  8. };
  9.  
  10. Collections.sort(list, comparator); // use the comparator as much as u want
  11. System.out.println(list);
  12.  
  13. public class CustomComparator implements Comparator<CustomObject>
  14. {
  15. @Override
  16. public int compare(CustomObject o1, CustomObject o2) {
  17. return o1.getId().compareTo(o2.getId());
  18. }
  19. }
  20.  
  21. Collections.sort(list, new CustomComparator());
  22.  
  23. Collections.sort(allMatching, new Comparator<ClassOne>() {
  24. @Override public int compare(final ClassOne o1, final ClassOne o2) {
  25. if (o1.getMethodToSort() > o2.getMethodToSort()) {
  26. return 1;
  27. } else if (o1.getMethodToSort() < o2.getMethodToSort()) {
  28. return -1;
  29. }
  30. return 0;
  31. }
  32. });
Add Comment
Please, Sign In to add comment