How to sort persistant data? [closed]
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)));
}
}
精彩评论