开发者

How to sort persistant data? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_StackOverflow社区 Closed 10 years ago.

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 comes, inside the persistence, our list is sorted.

Can someone help me?


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


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜