I have a statement:
searchResults.sort(Comparator.comparing(WCCTableRowData::getD));
where getD is an accessor in the class WCCTableRowData and searchResults is a list of WCCTableRowData. The WCCTableRowData class has accessors from getA through getZ. I need to be able to set the sort field on the fly from a passed in variable. Is there an elegant way to do this or will I need a series of if statements or similar?
UPDATE 1 Unfortunately, neither approach in the accepted answer worked though I think in general the direction is correct. With approach 2 I get:
With approach 1, row.getField does not pick up the getField method in WCCTableRowData class and I get similar "does not conform to upper bound(s)" error. I think the error is saying that WCCTableRowData class has to implement Comparable?

Function<WCCTableRowData, Comparable<?>>, call itextractor. Then you can simply dosearchResults.sort(Comparator.comparing(extractor)).Comparable<?>in solution #2 leads to severe type issues like the one you posted about. To prevent such issues, one may change type reference to justComparable(raw type) ingettersdefinition. You will get type warnings in variable definition, of course, but you may easily overcome those by prepending a @SuppressWarnings:@SuppressWarnings("rawtypes") Map<String, Function<WCCTableRowData, Comparable>> getters = ...Unchecked conversion warning incomparing()could be suppressed as well.getFieldtoComparable(Comparable getField(final String name) {...}) and you will have a working solution.