2
我有麻烦,使用SimpleSortingVector排序我的黑莓应用矢量。我的东西不排序它仍然是一样的。如何使用SimpleSortingVector对黑莓中的矢量进行排序?
这里是我迄今为止...
MyComparator类
private Vector vector = new Vector(); //Assume that this vector is populated with elements already
SimpleSortingVector ssv = new SimpleSortingVector();
ssv.setSortComparator(new Comparator() {
public int compare(Object o1, Object o2) {
Record o1C = (Record)o1;
Record o2C = (Record)o2;
return o1C.getName().compareTo(o2C.getName());
}
public boolean equals(Object obj) {
return compare(this, obj) == 0;
}
});
for(int i=0;i<vector.size();i++){
Record record = new Record();
record=(Record) vector.elementAt(i);
//when you add elements to this vector, it is to post to be automatically sorted
ssv.addElement(record);
}
类记录
public class Record {
String name;
int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
+ 1,是的,你说得对,我的回答是绝望的尝试。 – dreamcrash
它的工作原理。谢谢。它比较快,负载较小SimpleSortingVector.setSort(true)或SimpleSortingVector.reSort()? – ejobity
我看不到源代码,所以我只能做出有根据的猜测。两者应该有相同的渐近时间:O(n log n)。 reSort()应该能够以稍小的常数因子进行排序,因此速度会更快一些。 –