BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 07-17-2007, 04:44 AM   #1
seekay
Knows Where the Search Button Is
 
Join Date: Apr 2007
Model: 8700
PIN: N/A
Carrier: O2
Posts: 24
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, 07:49 AM   #2
seekay
Knows Where the Search Button Is
 
Join Date: Apr 2007
Model: 8700
PIN: N/A
Carrier: O2
Posts: 24
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 07:53 AM.. Reason: code formatting
Offline  
Old 07-17-2007, 08:47 AM   #3
bemshaswing
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
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, 07:42 PM   #4
seekay
Knows Where the Search Button Is
 
Join Date: Apr 2007
Model: 8700
PIN: N/A
Carrier: O2
Posts: 24
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


Grandstream GXP2160 VoIP IP Phone Color Gigabit Enterprise HD 6 Line PoE Black picture

Grandstream GXP2160 VoIP IP Phone Color Gigabit Enterprise HD 6 Line PoE Black

$37.98



Grandstream WP810 Dual Band Portable Wi-Fi Phone Voip Phone and Device- picture

Grandstream WP810 Dual Band Portable Wi-Fi Phone Voip Phone and Device-

$33.43



Polycom VVX 410 VoIP IP Phone & Stand Warranty Reset VVX410 2201-46162-001 Lync picture

Polycom VVX 410 VoIP IP Phone & Stand Warranty Reset VVX410 2201-46162-001 Lync

$17.98



NEW Polycom VVX 501 VoIP IP Phone & Stand Warranty VVX411 2200-48500-019 Lync picture

NEW Polycom VVX 501 VoIP IP Phone & Stand Warranty VVX411 2200-48500-019 Lync

$49.98



Polycom VVX 501 VoIP IP Phone & Stand Blem Warranty VVX501 2201-48500-001 picture

Polycom VVX 501 VoIP IP Phone & Stand Blem Warranty VVX501 2201-48500-001

$14.98



Polycom SoundStation IP 6000 VoIP Conference Phone - No cords, No Accessories picture

Polycom SoundStation IP 6000 VoIP Conference Phone - No cords, No Accessories

$18.00







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