I am new to Java and now I need to create some Comparator classes.
On this Stackoverflow page, I found very useful information on using lambda expressions. How to compare objects across multiple fields
What made me think about creating a Compartor class as follows:
public class WidthComparator implements Comparator{
@Override
public int compare(Object t, Object t1) {
Foto foto1 = (Foto)t;
Foto foto2 = (Foto)t1;
return Comparator.comparing(Foto::getWidth)
.thenComparing(Foto::getHeight)
.thenComparingInt(Foto::getName);
}
}
}
so when I have a collection called fotosCollection, I would like to be able to do this:
fotosCollection.sort(new HoogteComparator());
This obviously doesn't work, but how can I make it work?
Ps. I have to use the Comparator class.
source
share