views:

392

answers:

2

Hi All, Can you please help me in sorting persistent store records in Blackberry application? I want to sort it in ascending and descending order... Please help. Thanks in advance

+1  A: 

For sorting in blackberry you can use SimpleSortingVector but bad news is, SimpleSortingVector is not persistable. So for sorting in persistent store you have to write your own sorting method.

Vector info = (Vector) store.getContents();

now sort contents of vectors with any sorting algo.

Vivart
+1  A: 

I'm assuming you are referring to sorting records which are stored in the PersistentStore, not just objects which implement the Persistable interface.

In order to commit your data to the PersistentStore:

SimpleSortingVector ssv = new SimpleSortingVector();
// Construct your records and add them to the vector

PersistentObject po = PersistentStore.getPersistentObject(persistentStoreKey);
po.setContents(new ControlledAccess(ssv, codeSigningKey));
po.commit();

In order to retrieve the vector:

Object contents = po.getContents();
if (contents instanceof SimpleSortingVector)
{
    // Cast to a SimpleSortingVector and load your data
}

I believe there shouldn't be any issue with putting a SimpleSortingVector in the PersistentStore. However, it does blend the line between the storage of your data and it's behavior (since you are not only storing your data records, but also a Comparator and sorting information). It may be a cleaner solution to store your records in the PersistentStore as just a Vector, and when you load it out of the PersistentStore, copy it into a SimpleSortingVector which you use throughout the runtime of your app. If you went with that approach, you could store information about the Comparator and sorting information in the Vector itself in a specified format, and extract that out when you construct the SimpleSortingVector.

CJ
Hi, I want to sort persistent store records,by implementing a query like "Select TimeStamp from Table where ID = + someID order by Date desc limit 0,1" How do I order records in store and pick up latest date?
imMobile
You can put all the records in a SimpleSortingVector, and implement a comparator to use for it. The SimpleSortingVector will use the comparator's compare(Object o1, Object o2) method to sort the vector (you can sort by Date). To implement more sophisticated SQL-like queries, you would probably need something more capable.
CJ