我正在使用“ compareTo(String string);”对数组列表int项进行排序这种方法只比较字符串而不比较int值。如何根据整数值对数组列表进行排序。

这是我的代码:

 Collections.sort(mAllSpeedsArray, new AllSpeedsComparator());

public class AllSpeedsComparator implements Comparator<AllSpeeds> {

@Override
public int compare(AllSpeeds lhs, AllSpeeds rhs) {
    // TODO Auto-generated method stub
        return lhs.getSpeed().compareTo(rhs.getSpeed());
    }
}

最佳答案

这个怎么样

@Override
public int compare(AllSpeeds lhs, AllSpeeds rhs) {
    // TODO Auto-generated method stub
    int a = Integer.parseInt(lhs.getSpeed());
    int b = Integer.parseInt(rhs.getSpeed());
    return a < b ? 1 : (a == b ? 0 : -1);
}

07-27 21:09