BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 08-31-2009, 07:54 PM   #1 (permalink)
New Member
 
Join Date: Aug 2009
Model: 8330
PIN: N/A
Carrier: verizon
Posts: 2
Post Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Arrays.sort not working?

Please Login to Remove!

Arrays.sort appears to have a bug (?!?). Customer has 8330 device. My code is trying to sort an array of objects of type:

class CallInfo
{
String name;
String number;
int durationInSecs;
long dateInMillis;
:

So I create a Vector of these from bb phone log objects, then I have this code to turn the Vector into an array, to sort the array, and use my Log class to debug the results:

call = new CallInfo[v.size()];
for ( int n = 0; n < v.size(); n++ )
{
call[n] = (CallInfo) v.elementAt( n );
Log.log( "before sort " + call[n].dateInMillis + " " + call[n].toString() );
}
Arrays.sort( call, new CallInfoComparator() );

for ( int n = 0; n < v.size(); n++ )
{
Log.log( "after sort call " + call[n].dateInMillis + " " + call[n].toString() );
}

Here is the comparator:

class CallInfoComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
CallInfo c1 = (CallInfo) o1;
CallInfo c2 = (CallInfo) o2;
return (int) (c2.dateInMillis - c1.dateInMillis);
}
}

And it doesn't work. The array is initially in the reverse order of what I want (oldest entry at top, newest entry at bottom). And the output array winds up sorted in the correct order - but in two pieces. For example, if the array looked like this "before sort":

1
2
3
4
5
6

It would look like this "after sort call":

4
3
2
1
6
5

I found no forum posts about a bug in Arrays.sort, but it sure looks like there is one. The problem only shows up when there are like a hundred entries. I have attached the actual output from my Log.log method
Attached Files
File Type: txt testlogsort.txt (32.2 KB, 1 views)

Last edited by tommurphyusa : 08-31-2009 at 09:40 PM. Reason: clarification
Offline  
Old 08-31-2009, 10:24 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

I guess my first question is "why bother with the array?"

Create a SimpleSortingVector and add your phone logs objects to it. Your vector will be sorted at the end of this one operation.
Offline  
Old 09-01-2009, 05:19 AM   #3 (permalink)
New Member
 
Join Date: Aug 2009
Model: 8330
PIN: N/A
Carrier: verizon
Posts: 2
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Doug but I just figured it out. The comparator is overflowing. Need to compare it like this:

public int compare(Object o1, Object o2)
{
CallInfo c1 = (CallInfo) o1;
CallInfo c2 = (CallInfo) o2;
if ( c2.dateInMillis > c1.dateInMillis ) return 1;
else if ( c2.dateInMillis < c1.dateInMillis ) return -1;
else return 0;
}

Last edited by tommurphyusa : 09-01-2009 at 05:27 AM. Reason: better performing code
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

IOP-2S32-SD PHILIPS ADVANCE 120V TO 277V 50/60Hz START ELECTRONIC BALLAST
$7.0
IOP-2S32-SD PHILIPS ADVANCE 120V TO 277V 50/60Hz  START ELECTRONIC BALLAST pictureProLume Proformance Electronic Ballast EP2751IS/MV (2) USED
$52.0
ProLume Proformance Electronic Ballast EP2751IS/MV (2) USED pictureNEW NIB Philips Advance Core & Coil Ballast Kit 71A5593-001D Free Priority Ship
$39.99
NEW NIB Philips Advance Core & Coil Ballast Kit 71A5593-001D Free Priority Ship picture100 watt Metal Halide , Multi Tap Ballast 120V,208V,240V,277V
$23.0
100 watt Metal Halide , Multi Tap Ballast  120V,208V,240V,277V    pictureLOT OF 3 Lutron Hi-Lume 3D H3DT832CU210 Flourescent Dimming Ballast
$75.0
LOT OF 3 Lutron Hi-Lume 3D  H3DT832CU210 Flourescent Dimming Ballast   picture






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