BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 07-17-2007, 05:44 AM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Apr 2007
Model: 8700
PIN: N/A
Carrier: O2
Posts: 24
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorting records in RIM Persistent Store API

Please Login to Remove!

Hi!

Has anyone found/developed a class/method for sorting records stored in the Blackberry Persistent Store? I'm thinking something similar to the RMS RecordComparator class.

Specifically I'm storing hashtables as persistent objects via a custom class and would very much like to retrieve sorted sets of records, so even a method I can call like MyClass.getSortedRecords(PARAMS) would be very useful?

If anyone has seen/written anything like this I'd be very grateful...

Cheers!
Offline  
Old 07-17-2007, 08:49 AM   #2 (permalink)
Knows Where the Search Button Is
 
Join Date: Apr 2007
Model: 8700
PIN: N/A
Carrier: O2
Posts: 24
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok folks here's a further update.

I have a custom class for the store, lets call it MyStoreObject. I also have a custom class which represents and entire 'table' of data from the store called ObjectDB.

I have a method already which gets the whole hashtable and puts it into an array of a custom data type, E.g. I can do :
Code:
MyStoreObject myStore = new MyStoreObject();
ObjectDB[] objDBProducts = myStore.getAllDataObject(PRODUCTS);
Products[] prodObj = new Products[objDBProducts.length];

for (int i = 0; i < objDBProducts.length; i++) {
    prodObj[i] = (Products)objDBProducts[i]
}
Now the products object is defined in such a way that I can refer to the 'fields' of the object as prodObj[i].PRODUCT_ID for example.

Now I'm think I ought to be able to use the sort method on the prodObj[] array in order to sort the records by a given column, and I should be able to write a comparator to use to perform this sort, but I really don't know where to begin. I'm not a java developer (although I am a developer) and I've inherited this project. All my java expertise has come from unravelling this app and some lucky finds in the docs to develop new stuff, but now I've got stuck!

Any ideas (even code to point my syntax in the right direction) would be very much appreciated!

Cheers!

this will give me

Last edited by seekay : 07-17-2007 at 08:53 AM. Reason: code formatting
Offline  
Old 07-17-2007, 09:47 AM   #3 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's something I use to sort strings as floating point values. The J2ME library is thin so you have to use a quote comparator like so:

Code:
Object[] objectArray = new Object[data.size()];
			data.copyInto(objectArray);
			QuoteComparator qc = new QuoteComparator(sortingIndex);
			
			if (sortDirection != 0) {
				if (sortDirection != 1) {
					InvertedOrderComparator ioc = new InvertedOrderComparator(
							qc);
					Arrays.sort(objectArray, ioc);
				} else {
					Arrays.sort(objectArray, qc);
				}

				data = new Vector();
				for (int i = 0; i < objectArray.length; i++) {
					data.addElement(objectArray[i]);
				}
			}
this goes with
Code:
import java.util.Vector;

import net.rim.device.api.util.Arrays;
import net.rim.device.api.util.Comparator;

public class QuoteComparator implements Comparator {
	int index;

	public QuoteComparator(int sortIndex) {
		index = sortIndex;
	}

	public int compare(Object arg0, Object arg1) {
		try {
			String[] ar1 = (String[]) arg0;
			String[] ar2 = (String[]) arg1;
			String stringValue1 = ar1[index];
			String stringValue2 = ar2[index];
			if (stringValue1.equalsIgnoreCase("-"))
				stringValue1 = "0.0";
			if (stringValue2.equalsIgnoreCase("-")) 
				stringValue2 = "0.0";
			stringValue1 = FWUtils.replaceString(stringValue1, ",", "");
			stringValue2 = FWUtils.replaceString(stringValue2, ",", "");
			try {
				double val1 = Double.parseDouble(stringValue1);
				double val2 = Double.parseDouble(stringValue2);
				if (val1 < val2)
					return -1;
				else if (val1 == val2)
					return 0;
				else
					return 1;
			} catch (java.lang.NumberFormatException ex) {
				return stringValue1.compareTo(stringValue2);
			}

		} catch (Exception e) {
		
			return 0;
		}
	}

	

}
Hopefully this is of some help
Offline  
Old 07-17-2007, 08:42 PM   #4 (permalink)
Knows Where the Search Button Is
 
Join Date: Apr 2007
Model: 8700
PIN: N/A
Carrier: O2
Posts: 24
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks so much Bemshaswing!

This is exactly what I needed - I was able to modify slightly to use integers in the compare and am now quite confident I could modify further to include any data type.

Worked a treat!

Incidentally I think this is the first post I've had a reply to yet before I managed to figure it out myself, I look forward to being able to do the same for others when I get up and running 'proper' with this environment!

Thanks again!
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.