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



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


4 Count Case GE ProLine 2 Bulb 120V T12 Electronic Fluorescent Light Ballasts picture

4 Count Case GE ProLine 2 Bulb 120V T12 Electronic Fluorescent Light Ballasts

$49.99



PHILIPS ADVANCE Core-Coil Ballast Kit 71A5389- 1-100W M90 Metal Halide.FreeShip picture

PHILIPS ADVANCE Core-Coil Ballast Kit 71A5389- 1-100W M90 Metal Halide.FreeShip

$22.99



Hatch Ballast ESB-0848-46-UV picture

Hatch Ballast ESB-0848-46-UV

$150.00



UNIVERSAL LIGHTING TECHNOLIGIES HALIDE BALLAST LAMP M4OO120AC4M TRANSFORMER  picture

UNIVERSAL LIGHTING TECHNOLIGIES HALIDE BALLAST LAMP M4OO120AC4M TRANSFORMER

$30.00



Lutron Hi-lume 3D Electronic Fluorescent Dimming Ballast H3DT528CU210 picture

Lutron Hi-lume 3D Electronic Fluorescent Dimming Ballast H3DT528CU210

$49.99



Inter-Global IG13-20EL electronic ballast 120v New Common Aquarium Light Ballast picture

Inter-Global IG13-20EL electronic ballast 120v New Common Aquarium Light Ballast

$19.95







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