sort a list of objects based on runtime property
- by jijo
I have an arraylist of VOs. These objects have many properties and corresponding get/set methods. I want to sort this array list based on a property which I'll be getting in runtime.
Let me explain in detail. My VO is like this
public class Employee {
String name;
String id;
private String getName() {
return name;
}
private String getId() {
return id;
}
}
I will be getting a string ‘sort’ in runtime, which can be either ‘id’ of ‘name’. I want to sort the list based on the value of the string.
I have tried to use comparator and reflection together, but no luck. I don’t want to use an if loop and create new comparator classes. Any other thoughts?