Collections.max with custom Comparator on list

44 views Asked by At

I have an array list of Location objects that I want to find the max and min value of. However, when calling collections.max(listN, PopulationComparator) it gives me the minimum value whereas for collection.min() it returns the max value. The problem is resolved once changing the order to be descending, but I don't understand why this would even matter.

static class PopulationComparator implements Comparator <Location>{
    public int compare(Location a, Location b){ 

      // ascending order
        if(a.getPop() > b.getPop()){ 
            return 1;
        }else if(a.getPop() < b.getPop()){
            return -1;
        }else{
            return 0;
        }
    }

}

public static void main(String[] args){
Location maxPopulation = Collections.max(listN, new PopulationComparator());
}
0

There are 0 answers