关于ArraryList排序的一点记录

  在做关于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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值