Search code examples
javajava-mesorting

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 ?


Solution

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