Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 5    Views: 56

SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
06/05/13 03:12 PM (12 years ago)

Sort by Numbers... [solved]

Taking little bits from Susan, Chris1, Danny, and Mr. David I've been able to create my own little custom 'menu with image and search and sort' plugin for my location based app. Kind of. I now get my data as a json file, which includes geo-coordinates. I take those coordinates and compute 'distance', and reinsert that value in each location object. (Big Props to Chris1 for the info in this post which led me to the json disassembly/addition/reassembly: https://www.buzztouch.com/forum/thread.php?tid=64A97C87E9AD0EABAAE26EA&currentPage=3) What I'm trying to do is sort by distance. Currently sort is mitigated by a string value (sortableColumnValue) and what happens is my sort ends up being 1,12, 2, 20, 3, 30 and so on... of course I need it to be 1,2,3...10, 11, 12... and such. Since the 'BT_item' object uses a string for the value 'sortableColumnValue' I subclassed the BT_item object (into SW_item) and added another variable; 'sortableColumnNumber' and have been trying to sort by that. I've tried it as an NSNumber, I've tried it as an 'int', I've tried it as all kinds. But when I try to use it as a sort column, I get halted. No real 'error', it just stops on the line where I'm assigning my numerical value to my 'sortableColumnNumber'. Everything I've read leads me to believe this method 'can' work, but up to now I haven't been successful. Has anyone changed their method of sorting to numerical, and if so, would they be willing to share? Cheers! -- Smug
 
nadthevlad
Code is Art
Profile
Posts: 1025
Reg: Jun 07, 2012
Denver
21,850
like
06/05/13 04:13 PM (12 years ago)
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
06/05/13 04:21 PM (12 years ago)
I'll take a look... The whole conversion is like this: I have geo-coordinates for the 'location'. (lat, lng, each doubleValue) I have geo-coordinates for the users current location. (lat, lng, each doubleValue) I convert them to doubleValue for the computation 'getDistanceFrom' and receive a doubleValue result (as CLLocationDistance). From that point, I'm kind of lost. I've tried converting to string, and that's the only one that works so far, but I get 1,10,2,20 sorting. I 'think' I tried leading zeros, but I'll try again; I quit last night after a loooooong day of getting it all working. But I know that numbers will sort correctly if sorted as a number, not a string, so I was kind of hoping for that. But I'll take whatever works, lol! Cheers! -- Smug
 
mrDavid
BTMods.com
Profile
Posts: 3936
Reg: May 21, 2011
San Diego, CA
51,910
like
06/05/13 04:31 PM (12 years ago)
I have no idea if this is any good for you smug, but I believe that's called a "natural order" sort algorithm, maybe that will help your search. Cheers, David.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
06/05/13 04:34 PM (12 years ago)
And I can't find the StackOverflow post right now, but I found an interesting tidbit; Android computes distance correctly. Apple doesn't. In order to get the correct distance out of your iDevice, you have to do: distance1 = [loc1 getDistanceFrom:loc2]; distance2 = [loc2 getDistanceFrom:loc1]; avgDist = (distance 1 + distance2)/2; For some reason, distance is different depending on whether you go from A to B, or from B to A. The way to get the correct distance is to use both and divide the two. Odd, yes? Cheers! -- Smug Thanks David! I'll start on that!!
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
06/09/13 12:59 AM (12 years ago)
Got it! So simple like the jitterbug, it plum evaded me ;) Change: NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"sortableColumnValue" ascending:YES] autorelease]; to NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"sortableColumnValue" ascending:YES selector:@selector(localizedStandardCompare:)] autorelease]; and even though the number is a 'string' it will sort it in numerical order. *perfect*. Hope this saves some time for someone. Cheers! -- Smug
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.