One possible way of accomplish this is :
Create a class and add the needed properties to it. Then use Comparator to sort according to need.
I have given example of how to sort on id and name basis of class Student
public class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id=id;
this.name=name;
}
@Override
public String toString() {
return this.id+"-"+this.name;
}
public String getName(){
return this.name;
}
public static void main(String[] args) {
List<Student> stu = new ArrayList<>();
stu.add(new Student(4,"Alaska"));
stu.add(new Student(5,"Barlin"));
stu.add(new Student(9,"America"));
stu.add(new Student(2,"India"));
Comparator<Student> byId = (Student s1,Student s2)->(s1.id)-(s2.id); //java 8 lambda expression
stu.sort(byId);
System.out.println(stu);
//Way-1 -java 8 lambda expression
Comparator<Student> byName = (Student s1,Student s2)->s1.getName().compareTo(s2.getName());
//Way-2 - java 8 method reference
//Comparator<Student> byName = Comparator.comparing(Student::getName);
stu.sort(byName);
System.out.println(stu);
}
}
[2-India, 4-Alaska, 5-Barlin, 9-America]
[4-Alaska, 9-America, 5-Barlin, 2-India]