Long story short, I am using JDBI DAO to access data. It is hard to have a query with a dynamic order in it. So I am planning to use a Java 8 stream to perform this as a post processing step after the query results are fetched.
The problem is the way the comparator works by having to declare a method of the object statically.
shepherds = shepherds.stream()
.sorted(Comparator.comparing(Shepherd::getId).reversed())
.collect(Collectors.toList());
How can I do this dynamically with what will be variables like this
orderBy = id
orderDirection = ASC
So that I can paramaterize this method call?
e.g.
if(orderDirection.equals("ASC"))
shepherds.stream().sorted(Comparator.comparing(orderBy));
else
shepherds.stream().sorted(Comparator.comparing(orderBy).reversed());