I understand how to build and use Comparator
for lists of objects based on multiple attributes of those objects. However, I'm not sure how to do that efficiently by giving the user the choice of which parameters to sort by. This is a GWT
UI.
For example, I have two drop-down menus with 11 parameters each, corresponding to the attributes of the objects. Each menu also as a button beside to specify ascending/descending order.
So, the user may choose of example, to sort by fileName, desc
and timestamp, asc
. How can I sort these objects, without a ridiculously long if
statement for the few hundred possible combinations?
I could do this if the sort was fixed:
files.sort(Comparator.comparing(FileInfo::getFileName).thenComparing(FileInfo::getTimestamp).reversed());
(I might have got asc/desc backwards there)
But I shouldn't have to type that out for every conceivable option. So how should I do that?