您的位置:首页 > 其它

关于ArraryList排序的一点记录

2016-12-26 22:19 387 查看
  在做关于ArraryList排序的时候,借鉴网上的方法使用Collections来实现排序,结果与实际结果不相符,在此做一下记录,具体代码如下:

public class SortBitmap {
public Bitmap mBitmap;
public int mIndex;//记录图片的顺序位置
public SortBitmap(int index,Bitmap bitmap){
mBitmap = bitmap;
mIndex = index;
}
}

ArrayList<SortBitmap> sortBitmaps = new ArrayList<SortBitmap>()
sortBitmap1 = new SortBitmap(bitmap1,0);
sortBitmaps.add(sortBitmap1);
sortBitmap2 = new SortBitmap(bitmap2,1);
sortBitmaps.add(sortBitmap2);

Collections.sort(sortBitmaps, new SortByIndex());

for (SortBitmap sortBitmap : sortBitmaps) {
bitmaps.add(sortBitmap.mBitmap);
}


排序类:

class SortByIndex implements Comparator {
public int compare(Object o1, Object o2) {
SortBitmap s1 = (SortBitmap) o1;
SortBitmap s2 = (SortBitmap) o2;
if(s1 .mIndex < s2.mIndex){
return 1;
}
return 0;
}
}


  以上的代码很简单,就是对图片的索引值进行排序,然后在按排列好的顺序,把图片提取出来。结果发现每次提取的都是索引号为1的图片排在前面,索引号为0的排在后面,我以为接口中比较大小的部分错了,然后就有重新修改,把代码:

if(s1 .mIndex < s2.mIndex)


改为:

if(s1 .mIndex > s2.mIndex){


发现还是没有起到排序的作用。

最后发现在接口返回0的时候,不进行排序,最后修改的结果如下:

//排序
class SortByIndex implements Comparator {
public int compare(Object o1, Object o2) {
SortBitmap s1 = (SortBitmap) o1;
SortBitmap s2 = (SortBitmap) o2;
return (s1.mIndex - s2.mIndex);
}
}


特此在这记录一下。网上有些例子就是返回0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: