tags:

views:

35

answers:

2

hi , i am making a application in which i am storing many dates in long in persistant object. i want to make a list of these long value in asending order,whenever a new value come inside the persistance our list is sorted.. plz give me code.. thanking you..

+3  A: 

Use net.rim.device.api.util.SimpleSortingVector to store your data.

oxigen
+1  A: 

Like oxigen said, use the SimpleSortingVector... which is not simple at first glance!

You have to create a comparator class to pass into the sorting vector. See example:

// Your hashtable with key value pairs
Hashtable data = getMyHashTableWithSomeDataInIt();

// Your sorting vector
SimpleSortingVector sorted = new SimpleSortingVector();

// Iterate through you hashtable and add the keys to the sorting vector
for (Enumeration e = data.keys(); e.hasMoreElements();)
{
    String key = (String) e.nextElement();
    sorted.addElement(key);
}

// Pass in the comparator and sort the keys
sorted.setSortComparator(new MyComparator());
sorted.reSort();

// Iterate through your sorted vector
for (Enumeration e = sorted.elements(); e.hasMoreElements();)
{
    String key = (String) e.nextElement();
    Object sortedItem  = (Object) data.get(key);

    // Do whatever you have to with the sortedItem
}

// Sort compartor for sorting strings
class MyComparator implements Comparator {
    public int compare(Object k1, Object k2) {
        return ((((String) k1).compareTo((String) k2)));
    }
}
coryjacobsen