I am trying to achieve a sort for object's field value stored in List.
I found the following solution to compare the string but How I can compare the String value and sort accordingly?
I am looking to sort the "Y" status values first then "N"
class Student {
int rollno;
String name, status;
// Constructor
public Student(int rollno, String name,
String status) {
this.rollno = rollno;
this.name = name;
this.status = status;
}
public String getStatus() {
return status;
}
}
ArrayList < Student > ar = new ArrayList < Student > ();
ar.add(new Student(111, "bbbb", "Y"));
ar.add(new Student(131, "aaaa", "N"));
ar.add(new Student(121, "cccc", "Y"));
Collections.sort(ar, (a, b) - > a.getStatus().compareTo(b.getStatus()));