2

I am having trouble to sort Vector for my blackberry app using SimpleSortingVector. My stuff does not sort it remains the same.

here is what i have so far...

MyComparator class

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

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

}

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
ejobity
  • 305
  • 1
  • 3
  • 13

1 Answers1

4

SimpleSortingVector does not sort by default. This was unexpected for me the first time I ran into it, too, given the name of the class.

You can do one of two things. Call SimpleSortingVector.setSort(true) to make sure the vector is always sorted after each change. This is surprisingly not turned on by default. Or, you can call SimpleSortingVector.reSort() after adding all the elements to the vector, to do the sort in one batch operation.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
  • it works. thank you. which is faster in comparison and less load SimpleSortingVector.setSort(true) or SimpleSortingVector.reSort()? – ejobity Dec 01 '12 at 00:10
  • 1
    I can't see the source code, so I can only make an educated guess. Both should have the same asymptotic time: O(n log n). reSort() should be able to sort with a slightly smaller constant factor, so will be a little bit faster. – Michael Donohue Dec 01 '12 at 05:45