Java – sort the vectors of custom objects
•
Java
How to sort the vectors of custom objects and select the attributes to sort?
I do see this question & answer, but I'm not sure what its ranking is based on Code examples will take precedence over methodology
Sort a Vector of custom objects
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;
}
Solution
The following is an example that allows you to sort by the 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);
}
}
});
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
