Guest User

Untitled

a guest
Feb 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. public class Student {
  2. private String id;
  3. private String name;
  4. private int Batch;
  5.  
  6. //Added a class constructor getters and setters
  7.  
  8. public static Comparator<Student> studBatch = new Comparator<Student>() {
  9.  
  10. public int compare(Student s1, Employee s2) {
  11.  
  12. int batch1 = s1.getBatch();
  13. int batch2 = s2.getBatch();
  14.  
  15. //For ascending order
  16. return (int) (batch1-batch2);
  17. }};
  18. }
  19.  
  20. ArrayList<Student> stud = new ArrayList<>();
  21.  
  22. stud.add(new Student("A01", "John Doe", 101));
  23.  
  24. stud.add(new Student("A02", "Mike Gaman", 102));
  25.  
  26. Collections.sort(stud, stud.studBatch);
Add Comment
Please, Sign In to add comment