Im trying to sort through an arraylist of objects by a particular value within the object. What would be the best approach to do such a thing. Should I use Collections.sort() with some kind of comparator?
Im trying to sort a list of objects by a float value they hold in one of the variables.
EDIT:
This is what I have so far:
public class CustomComparator implements Comparator<Marker> {
@Override
public int compare(Mark o1, Mark o2) {
return o1.getDistance().compareTo(o2.getDistance());
}
}
the error states: Cannot invoke compareTo(double) on the primitive type double.
Is it because a comparator cant return anything other than a certain type?
You should use Comparable instead of a Comparator if a default sort is what your looking for.
See here, this may be of some help – When should a class be Comparable and/or Comparator?
Try this –