Java 比较器实现无法排序
我已经实现了一个Comparator类来根据列表元素的大小对其进行排序。守则如下:Java 比较器实现无法排序,java,android,comparator,Java,Android,Comparator,我已经实现了一个Comparator类来根据列表元素的大小对其进行排序。守则如下: public class SizeComparator implements Comparator< AppModel> { @Override public int compare( AppModel lhs, AppModel rhs ) { double i1 = lhs.getCache(); double i2 = rhs.getCach
public class SizeComparator implements Comparator< AppModel> {
@Override
public int compare( AppModel lhs, AppModel rhs ) {
double i1 = lhs.getCache();
double i2 = rhs.getCache();
if ( i1 > i2 )return +1;
else if (i1 < i2 )return -1;
return 0;
}
}
但这些应用程序仍然没有根据大小进行分类。不知道我错在哪里。有人能帮我吗?你得到了什么结果?是否所有项目都未排序,或其中一些项目已排序?元素的原始顺序是否没有变化?请分享更多详细信息。即使您的代码在功能上正确,也应将其替换为Double.comparelhs.getCache、rhs.getCache,因为这样就不会出错。代码看起来不错,提供更多信息可能是getCache没有返回所需的大小?但是如果getCache方法没有返回稳定的值,则排序无法工作。getCache究竟返回什么?如果可行的话,我们可以看看实施情况吗?
Collections.sort(installedApps, new SizeComparator());