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