Let´s say I´ve got a class called Product
, with the fields price
and weight
.
My final objective is to have a list of products that I can sort by price or weight
I know I can make Product implements Comparable<Product>
. That means I will have to, for instance:
@Override
public int compareTo(Product compareProduct) {
int comparePrice = ((Product) compareProduct).getPrice();
//ascending order
return this.price - comparePrice;
}
This code will only compare by price, so, in a nutshell, the question is how can I choose the compare method I want to use? (byPrice or byWeight)