aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/sorting/PopularityRatioComparator.java
blob: 4c2d9149b24e584a6a82cc2b200a1be2d53e893b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
 *
 */
package cgeo.geocaching.sorting;

import cgeo.geocaching.Geocache;

/**
 * sorts caches by popularity ratio (favorites per find in %).
 */
class PopularityRatioComparator extends AbstractCacheComparator {

    @Override
    protected int compareCaches(final Geocache cache1, final Geocache cache2) {
        final int finds1 = cache1.getFindsCount();
        final int finds2 = cache2.getFindsCount();

        float ratio1 = 0.0f;
        if (finds1 != 0) {
            ratio1 = (((float) cache1.getFavoritePoints()) / ((float) finds1));
        }
        float ratio2 = 0.0f;
        if (finds2 != 0) {
            ratio2 = (((float) cache2.getFavoritePoints()) / ((float) finds2));
        }

        if ((ratio2 - ratio1) > 0.0f) {
            return 1;
        }
        if ((ratio2 - ratio1) < 0.0f) {
            return -1;
        }

        return 0;
    }
}