自定义比较器不起作用

时间:2013-11-14 11:11:05

标签: android listview comparator

我正在使用ComparatorListView进行排序,但它不起作用。

我的代码:

Collections.sort(orgi, new Comparator<Loc>() {

                @Override
                public int compare(Loc lhs, Loc rhs) {

                    if( lhs.getDist() < rhs.getDist() )
                         return 1;
                      else
                         return 0;
                }
            });

有人可以建议解决方案吗?

1 个答案:

答案 0 :(得分:3)

试试这个:

Collections.sort(orgi, new Comparator<Loc>() {

            @Override
            public int compare(Loc lhs, Loc rhs) {
                if(lhs.getDist() < rhs.getDist()){
                     return -1;
                 } else if(lhs.getDist() > rhs.getDist()){
                     return 1;
                 } else {
                     return 0;
                 }
            }
        });