2

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 are "sorted" in the Vector. How to achieve this String sort ?

2
  • Hey guys he asking for java-me. Java-me never support Collections. Commented Jul 4, 2011 at 11:11
  • @Andy: 1st of ask the question clearly. Because everyone considered you are asking in java. Try to post properly. Commented Jul 4, 2011 at 11:15

8 Answers 8

7

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

3

If it's Java, are you looking for

Collections.sort(vector);

?

Comments

3

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.

1 Comment

The OP did not specify the way the keys will be sorted, I posted this in case the default one does not do the trick.
2

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.

Comments

1

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.

Comments

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

}
}

1 Comment

Don't forget to add reference link: java-examples.com/sort-elements-java-vector-example
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.