我有一个包含位置距离的列表视图,如下所示:
100
200
300
400
500
50
600
70
40
我需要按升序和降序对这个地方的距离进行排序。所以我所做的就是对适配器进行排序并使用adapter.notifyDataSetChanged()。如果距离是 3 位数字,即 230、450、352、254 等,则一切正常。但是如果位置距离是 2 位数,例如 52、65、75 等,我得到的排序值是错误的,如下所示。
对于升序:排序后我得到这个顺序
100
200
300
400
500
600
50
60
70
对于降序:排序后我得到这个顺序
70
60
50
600
500
400
300
200
100
我的排序代码如下所示:
case R.id.action_sort_dist_fn:
adapter.sort(new Comparator<Place>() {
@Override
public int compare(Place lhs, Place rhs) {
return rhs.getPlaceDistance().compareTo(
lhs.getPlaceDistance());
};
});
adapter.notifyDataSetChanged();
break;
case R.id.action_sort_dist_nf:
adapter.sort(new Comparator<Place>() {
@Override
public int compare(Place lhs, Place rhs) {
return lhs.getPlaceDistance().compareTo(
rhs.getPlaceDistance());
};
});
Place.java
public class Place {
String placeName = "";
String placeDistance="";
String placeCategoryIcon;
public Place(String placeName, String placeDistance,
String placeCategoryIcon) {
this.placeName = placeName;
this.placeDistance = placeDistance;
this.placeCategoryIcon = placeCategoryIcon;
}
public String getPlaceName() {
return placeName;
}
public void setPlaceName(String placeName) {
this.placeName = placeName;
}
public String getPlaceDistance() {
return placeDistance;
}
public void setPlaceDistance(String placeDistance) {
this.placeDistance = placeDistance;
}
public String getPlaceCategoryIcon() {
return placeCategoryIcon;
}
public void setPlaceCategoryIcon(String placeCategoryIcon) {
this.placeCategoryIcon = placeCategoryIcon;
}
}