开发者

How to sort a Vector of String in java-me?

开发者 https://www.devze.com 2023-03-17 15:44 出处:网络
I have a Vector of Integer ( primary key of a database table ) ; and I implemented a method which returns a String based on this primary key Integer. My problem is that I want to put these String\'s i

I have a Vector of Integer ( primary key of a database table ) ; and I implemented a method which returns a String based on this primary key Integer. My problem is that I want to put these String's into a Vector and they ar开发者_高级运维e "sorted" in the Vector. How to achieve this String sort ?


Use the following code for sort the vector in java-me.

public Vector sort(Vector sort) {
        Vector v = new Vector();
        for(int count = 0; count < e.length; count++) {
            String s = sort.elementAt(count).toString();
            int i = 0;
            for (i = 0; i < v.size(); i++) {
                int c = s.compareTo((String) v.elementAt(i));
                if (c < 0) {
                    v.insertElementAt(s, i);
                    break;
                } else if (c == 0) {
                    break;
                }
            }
            if (i >= v.size()) {
                v.addElement(s);
            }
        }
        return v;
    }


You can either use a TreeSet, which will keep Strings in sorted order, or use something like this before you return them:

Collections.sort(yourVector);

Another alternative is to ask the database to ORDER BY primary key. Let the database do the work.

Your query is probably returning more than the primary keys. I'd wonder why you're dealing with things on the primitive level of a String and primary key. I'll bet you're not thinking enough in terms of objects. Where there's a primary key, other data should be following close behind. I'd encapsulate all of them together and worry about sorting those objects.

Why Vector? I'd prefer ArrayList, because it's not synchronized by default. Better performance.


You will need to implement your own comparator by implementing the Comparator interface.

Something like this should do the trick:

Collections.sort(vect, new Comparator() {
  public int compare(Object a, Object b) {
    return ( new Integer(((MyClass) a).getNumber()) ).compareTo( new Integer(((MyClass) b).getNumber()));
  }
});

Taken from here.


Pass the vector to the static method Collections.sort(vector) not sure how you want it sorted but this method will sort according to the natural order of the objects contained in the vector, given by the compareTo method of each object.

The Collections API may be able to help you out further.


If it's Java, are you looking for

Collections.sort(vector);

?


Vector is slower than ArrayList as its a thread safe collection.

However the sort operation is not thread safe. As such to sort a vector in a thread safe manner you need to synchronize it.

synchronized(vector) {
    Collections.sort(vector);
}

If you don't need the collection to be thread safe, consider using an ArrayList.


import java.util.Vector;
import java.util.Collections;

public class SortJavaVectorExample {

public static void main(String[] args) {

//create Vector object
Vector v = new Vector();

//Add elements to Vector
v.add("1");
v.add("3");
v.add("5");
v.add("2");
v.add("4");

/*
  To sort a Vector object, use Collection.sort method. This is a
  static method. It sorts an Vector object's elements into ascending order.
  */
Collections.sort(v);

//display elements of Vector
System.out.println("Vector elements after sorting in ascending order : ");
for(int i=0; i<v.size(); i++)
System.out.println(v.get(i));

}
}


You can either use a Collections class, which will keep Strings in sorted order, or use something like this before you return them:

if you want to sort it in ascending order use only

Collections.sort(put your....LIST/ARRAYLIST/VECTOR OBJECT HERE);

if you want to sort it in descending order use only

** Collections.sort(put your....LIST/ARRAYLIST/VECTOR OBJECT HERE);** ** Collections.reverse(put your....LIST/ARRAYLIST/VECTOR OBJECT HERE);**

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号