对自定义对象的矢量进行排序 [英] Sort a vector of custom objects
本文介绍了对自定义对象的矢量进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何对自定义对象的矢量进行排序并选择要排序的属性?
How can I sort a vector of my custom object and choose which property to sort by?
我确实看到了这个问题&回答,但我不太清楚它的排序是基于什么的。代码示例优先选择方法论。
I did see this question & answer but I'm not too sure what its sorting it based on. Code example would be prefered to "methodology".
public class ItemLocation {
String icon;
String title;
String message;
String subtext;
String deviceId;
double latCoords;
double lngCoords;
int expiary;
int id;
double proximity;
String locSeen;
}
推荐答案
以下是一个示例允许您按ItemLocation的指定字段排序:
Below is a example that will allow you to sort by a specified field of ItemLocation:
public void sort(final String field, List<ItemLocation> itemLocationList) {
Collections.sort(itemLocationList, new Comparator<ItemLocation>() {
@Override
public int compare(ItemLocation o1, ItemLocation o2) {
if(field.equals("icon")) {
return o1.icon.compareTo(o2.icon);
} if(field.equals("title")) {
return o1.title.compareTo(o2.title);
} else if(field.equals("message")) {
return o1.message.compareTo(o2.message);
}
.
. fill in the rest of the fields...
.
else if(field.equals("locSeen")) {
return o1.locSeen.compareTo(o2.locSeen);
}
}
});
}
这篇关于对自定义对象的矢量进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文