I\'m trying to sort a list of Objects based upon user input. How can I go about having the sort method implement a variant comparator?
Example:
List<
This will helps you:
String sortBy = "key"; list.sort(Comparator.comparing(report -> { try { return (Comparable) report.getClass().getDeclaredField(sortBy).get(report); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("exception", e); } }));