集合排序 - 大型列表中的错误

时间:2017-11-19 15:41:17

标签: android sorting arraylist collections

我试图按页码排序对象列表(如果页面相同,按位置排序),如下所示:

Collections.sort(arlToTort, new Comparator<UserDefinedLink>() {
@Override
public int compare(UserDefinedLink lhs, UserDefinedLink rhs) {
    if(lhs.getPageNum() > rhs.getPageNum()){
        return 0;
    }else {
        if(lhs.getPageNum() == rhs.getPageNum()){
            double rhsY =  getLinkYLocation(rhs.getLocationStart());
            double lhsY =  getLinkYLocation(lhs.getLocationStart());
            if(rhsY > lhsY){
                return 0;
            }else{
                return -1;
            }
        }else{
            return -1;
        }
    }
  }
});

当我设置一个小列表(少于32个项目)时它工作正常但是当列表最多32个项目时排序不正常,这里是排序后的日志示例:

列出31项:

 D/getSortedListByPageNumber: Page: 2 
 D/getSortedListByPageNumber: Page: 4
 D/getSortedListByPageNumber: Page: 4
 D/getSortedListByPageNumber: Page: 9
 D/getSortedListByPageNumber: Page: 9
 D/getSortedListByPageNumber: Page: 9
 D/getSortedListByPageNumber: Page: 9
 D/getSortedListByPageNumber: Page: 9
 D/getSortedListByPageNumber: Page: 9
 D/getSortedListByPageNumber: Page: 9
 D/getSortedListByPageNumber: Page: 10
 D/getSortedListByPageNumber: Page: 10
 D/getSortedListByPageNumber: Page: 10
 D/getSortedListByPageNumber: Page: 10
 D/getSortedListByPageNumber: Page: 10
 D/getSortedListByPageNumber: Page: 10
 D/getSortedListByPageNumber: Page: 14
 D/getSortedListByPageNumber: Page: 14
 D/getSortedListByPageNumber: Page: 20
 D/getSortedListByPageNumber: Page: 25
 D/getSortedListByPageNumber: Page: 26
 D/getSortedListByPageNumber: Page: 26
 D/getSortedListByPageNumber: Page: 28
 D/getSortedListByPageNumber: Page: 28
 D/getSortedListByPageNumber: Page: 30
 D/getSortedListByPageNumber: Page: 31
 D/getSortedListByPageNumber: Page: 34
 D/getSortedListByPageNumber: Page: 35
 D/getSortedListByPageNumber: Page: 39
 D/getSortedListByPageNumber: Page: 42
 D/getSortedListByPageNumber: Page: 44

列出32项:

D/getSortedListByPageNumber: num of items : 32
D/getSortedListByPageNumber: Page: 4
D/getSortedListByPageNumber: Page: 4
D/getSortedListByPageNumber: Page: 9
D/getSortedListByPageNumber: Page: 9
D/getSortedListByPageNumber: Page: 9
D/getSortedListByPageNumber: Page: 9
D/getSortedListByPageNumber: Page: 9
D/getSortedListByPageNumber: Page: 9
D/getSortedListByPageNumber: Page: 26
D/getSortedListByPageNumber: Page: 26
D/getSortedListByPageNumber: Page: 28
D/getSortedListByPageNumber: Page: 28
D/getSortedListByPageNumber: Page: 30
D/getSortedListByPageNumber: Page: 34
D/getSortedListByPageNumber: Page: 39
D/getSortedListByPageNumber: Page: 44
D/getSortedListByPageNumber: Page: 2
D/getSortedListByPageNumber: Page: 9
D/getSortedListByPageNumber: Page: 9
D/getSortedListByPageNumber: Page: 10
D/getSortedListByPageNumber: Page: 10
D/getSortedListByPageNumber: Page: 10
D/getSortedListByPageNumber: Page: 10
D/getSortedListByPageNumber: Page: 10
D/getSortedListByPageNumber: Page: 10
D/getSortedListByPageNumber: Page: 14
D/getSortedListByPageNumber: Page: 14
D/getSortedListByPageNumber: Page: 20
D/getSortedListByPageNumber: Page: 25
D/getSortedListByPageNumber: Page: 31
D/getSortedListByPageNumber: Page: 35
D/getSortedListByPageNumber: Page: 42

1 个答案:

答案 0 :(得分:4)

似乎你错过了一些约束,compareTo返回

Returns a negative integer => object is less than
Returns a zero => object are equals
Returns a positive integer => object is greater than

所以当对象为greater时,它应该返回正值,而不是0equality

所以使用

Collections.sort(arlToTort, new Comparator<UserDefinedLink>() {
        @Override
        public int compare(UserDefinedLink lhs, UserDefinedLink rhs) {
            if(lhs.getPageNum() > rhs.getPageNum()){
                return 1;
               //      ^ greater 
            }else {
                // for equality 
                if(lhs.getPageNum() == rhs.getPageNum()){
                    double rhsY =  getLinkYLocation(rhs.getLocationStart());
                    double lhsY =  getLinkYLocation(lhs.getLocationStart());
                    if(rhsY > lhsY){
                        return 1;
                    }
                    else if(rhsY == lhsY){ return 0;}
                    else{
                        return -1;
                    }
                }else{
                    return -1;
                }
            }
        }
    });