Codename One:用Integer / int方法编写排序比较

时间:2017-04-10 08:36:52

标签: codenameone

Codename One不接受原始类型(int),因此我尝试使用Integer编写以下比较器:

private static class CelebrityPopularityComparator implements Comparator<Celebrity> {
    @Override
    public int compare(Celebrity celebrity1, Celebrity celebrity2) {
        Integer celebrity1PopularitySum = celebrity1.getCelebrityPopularitySum();
        Integer celebrity2PopularitySum = celebrity2.getCelebrityPopularitySum();
        Integer comparisonResult = Integer.valueOf(celebrity1PopularitySum.compareTo(celebrity2PopularitySum));
        return comparisonResult;
    }
}

我尝试了许多方法,但它只是不起作用。它不会将构建发送到服务器。我该怎么办才能修复它?谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个:

        Integer celebrity1PopularitySum = celebrity1.getCelebrityPopularitySum();
        Integer celebrity2PopularitySum = celebrity2.getCelebrityPopularitySum();
        if(celebrity1PopularitySum.equals(celebrity2PopularitySum)){
            return 0;
        }else if(celebrity1PopularitySum.intValue() < celebrity2PopularitySum.intValue()){
            return -1;
        }else{
            return 1;
        }