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
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.
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.