Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132938 views
License: OTHER
1
import java.util.Comparator;
2
3
public class PopulationDensityComperator implements
4
Comparator<Country> {
5
6
@Override
7
public int compare(Country o1, Country o2) {
8
double o1Density = o1.population / o1.area;
9
double o2Density = o2.population / o2.area;
10
11
if (Math.abs(o1Density - o2Density) < 0.00001) {
12
return 0;
13
} else {
14
return o1Density - o2Density;
15
}
16
}
17
18
}
19
20