public class WorthComparator implements Comparator<Värdesak> {
public int compare(Värdesak v1, Värdesak v2) {
if (v1 instanceof Apparat && v2 instanceof Apparat) {
return ((Apparat) v1).worth() > ((Apparat) v2).worth() ? 1
: ((Apparat) v1).worth() < ((Apparat) v2).worth() ? -1
: 0;
} else if (v1 instanceof Smycke && v2 instanceof Smycke) {
return ((Smycke) v1).worth() > ((Smycke) v2).worth() ? 1
: ((Smycke) v1).worth() < ((Smycke) v2).worth() ? -1
: 0;
} else if (v1 instanceof Aktie && v2 instanceof Aktie) {
return ((Aktie) v1).worth() > ((Aktie) v2).worth() ? 1
: ((Aktie) v1).worth() < ((Aktie) v2).worth() ? -1 : 0;
}
}
}
As you can see I'm trying to compare different objects from an ArrayList to then sort them in my GUI by highest value. Each different object ("Smycke", Aktie, Apparat") is in their own respective subclass which each has a method for estimate their value. "Värdesak" is the superclass.
My problem is that I don't know who I will get a return-statement to this or is there's another smart way to do it?