user806574
user806574

Reputation:

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

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 ?

Upvotes: 2

Views: 14290

Answers (8)

MOHAMMAD S HUSSAIN
MOHAMMAD S HUSSAIN

Reputation: 17

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

Upvotes: 0

bharath
bharath

Reputation: 14453

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

Upvotes: 7

Rupok
Rupok

Reputation: 2082

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

}
}

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533492

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.

Upvotes: 1

npinti
npinti

Reputation: 52185

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.

Upvotes: 3

cdegroot
cdegroot

Reputation: 1805

If it's Java, are you looking for

Collections.sort(vector);

?

Upvotes: 3

adamjmarkham
adamjmarkham

Reputation: 2164

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.

Upvotes: 2

duffymo
duffymo

Reputation: 308753

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.

Upvotes: 3

Related Questions