I'm stuck in a situation where I only got Fields (java.lang.reflect) of a given class.
To help you better imagine, let's say we have code:
List<Apple> sortApplesByFieldName(List<Apple> apples, String fieldName) {
Field field = Apple.class.getDeclaredField(fieldName);
// some stream logic for sorting using this var
return apples;
}
Note, the class Apple is a regular POJO with private fields and public getters/setters.
Scanner
?apples
list in-place. You may want that, but I thought I'd mention it just in case you don't (and if you don't then useList#sort(Comparator)
).