I'm playing around with the Comparisonchain using this example. But it just shows one way to sort these objects.
public class Fruit implements Comparable<Fruit> {
private String name;
private String family;
private int calories;
@Override
public int compareTo( Fruit otherFruit ) {
return ComparisonChain.start()
.compare( name, otherFruit.name )
.compare( family, otherFruit.family )
.compare( calories, otherFruit.calories )
.result();
}
}
I was wondering now if it is possible to sort based on the needs of the end user. How would I have to implement it, if the sorting criteria are not fixed? E.g. Someone wants to sort first by name, then by calories and that's it. The next one wants to sort by familiy, then by calories and then by name. and so on. People might also want to sort descending instead of ascending.
Can anybody give me some hints on how to solve it?