ios - Sort Array by Distance for TableView -


can please me sorting array distance , display in tableview? have created property "sortedarray" , synthesized. i've created "sortlist" method , called under didupdatetolocation. doesn't seem sorting array or i'm not getting updated results tableview.

@property(nonatomic, strong) nsarray *sortedarray; -(nsarray *) sortlist; -(nsmutablearray *) barlist; 

@synthesize sortedarray; 

- (void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation {     currentloc = newlocation;      if (newlocation.horizontalaccuracy <= 100.0f) {         [locmgr stopupdatinglocation];     }     nslog(@"lat: %f, long: %f", currentloc.coordinate.latitude, currentloc.coordinate.longitude);      [self barlist];     [self sortlist];     [self.tableview reloaddata]; }   -(nsmutablearray *) barlist{     theauthors = [[nsmutablearray alloc] init];     @try {         nsfilemanager *filemgr = [nsfilemanager defaultmanager];         nsstring *dbpath = [[[nsbundle mainbundle] resourcepath ]stringbyappendingpathcomponent:@"tulsabars.sqlite"];         bool success = [filemgr fileexistsatpath:dbpath];         if(!success)         {             nslog(@"cannot locate database file '%@'.", dbpath);         }         if(!(sqlite3_open([dbpath utf8string], &db) == sqlite_ok))         {             nslog(@"an error has occured: %@", sqlite3_errmsg(db));          }           const char *sql = "select *  tulsabars";         sqlite3_stmt *sqlstatement;         if(sqlite3_prepare(db, sql, -1, &sqlstatement, null) != sqlite_ok)         {             nslog(@"problem prepare statement:  %@", sqlite3_errmsg(db));         }else{              while (sqlite3_step(sqlstatement)==sqlite_row) {                 author * author = [[author alloc] init];                 author.barname = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement,1)];                 author.baraddress = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement,2)];                 author.barstate = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement, 5)];                 author.barlat = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement, 8)];                 author.barlong = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement, 9)];                  if (currentloc == nil) {                     nslog(@"current location nil %@", currentloc);                 }else{                  cllocation *barlocation = [[cllocation alloc] initwithlatitude:[author.barlat doublevalue] longitude:[author.barlong doublevalue]];                 cllocationdistance distancekm = [currentloc distancefromlocation: barlocation]/1000;                 nsstring *distancestring = [[nsstring alloc] initwithformat: @"%f", distancekm];                 author.cacheddist = distancestring;                  [theauthors addobject:author];                  }             }         }         sqlite3_finalize(sqlstatement);     }     @catch (nsexception *exception) {         nslog(@"problem prepare statement:  %@", sqlite3_errmsg(db));     }     @finally {         sqlite3_close(db);          return theauthors;     } }   -(nsarray *) sortlist{      nssortdescriptor *descriptor = [[nssortdescriptor alloc] initwithkey:@"cacheddist"  ascending:yes];     sortedarray = [theauthors sortedarrayusingdescriptors:[nsarray arraywithobject:descriptor]];      return sortedarray; } 

um... -sortlist method isn't doing in-place sorting, it's returning sorted array, throwing away.

actually, same true -barlist method.

edit: wasn't quite true. -barlist in fact repeatedly overwriting ivar, though it's not clear whether intentional.

the upshot of that:

[self barlist]; [self sortlist]; 

does nothing spin cpu cycles - you're not doing return values!

edit: ok, see sort (subtly) setting ivar, it's confusing because it's returning sorted array. see comments below.

(this looks overly complicated way sort geodata, i'm not going wade through code right now. need fix above before looking @ that.)


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -