我假设您有一个绑定到适配器的图像列表。如果您有一个图像列表并希望对其进行排序,则需要实现自定义比较器来对该数组(列表)进行排序。
这是自定义比较器的示例...
public Comparator<TheaterInfo> ComparatorByDistance = new Comparator<TheaterInfo>() {
public int compare(TheaterInfo object1, TheaterInfo object2) {
if (object1.getDistance().length() > 0
&& object2.getDistance().length() > 0) {
String[] d1 = object1.getDistance().replace(" ", "#").split("#");
String[] d2 = object2.getDistance().replace(" ", "#").split("#");
float o1 = Utils.ConvertToFloat(d1.length > 1 ? d1[0] : "0.0");
float o2 = Utils.ConvertToFloat(d2.length > 1 ? d2[0] : "0.0");
if (o1 == o2)
return 0;
return o1 > o2 ? 1 : -1;
}
else
{
return object1.getDistance().length() > 0 ? 1 : -1;
}
}
};
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式使用此比较器...
Collections.sort(your_list_type_object, ComparatorByDistance);
Run Code Online (Sandbox Code Playgroud)
谢谢。
| 归档时间: |
|
| 查看次数: |
4413 次 |
| 最近记录: |