如何使用SimpleSortingVector对黑莓中的向量进行排序



我无法使用SimpleSortingVector对我的黑莓应用程序进行Vector排序。我的东西没有排序,它保持不变。

这是我到目前为止所拥有的...

我的比较器类

    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;
    }

}

SimpleSortingVector 默认不排序。 考虑到班级的名称,当我第一次遇到它时,这对我来说也是出乎意料的。

您可以执行以下两项操作之一。 调用 SimpleSortingVector.setSort(true) 以确保每次更改后始终对向量进行排序。 令人惊讶的是,默认情况下没有打开此功能。或者,您可以在将所有元素添加到向量后调用 SimpleSortingVector.reSort(),以在一个批处理操作中执行排序。

最新更新