2010-12-15 104 views
0

我從SQLite表中檢索行,然後動態創建一個對象,然後將該對象添加到NSMutableArray。我的問題是,我無法找出一種方法來動態排序基於距離屬性(這是一個雙精度)的數組中的對象,因爲我將每個對象添加到for循環中的NSMutableArray,從最小值到最大值最大。如何動態地對Objective-C中的NSMutableArray進行排序?

我的相關代碼如下所示:

sqlite3 *database; 

//Init the restaurant array 
restaurants = [[NSMutableArray alloc] init]; 

CLLocation *firstLocation = [[[CLLocation alloc] initWithLatitude:userLatitude longitude:userLongitude] autorelease]; 

//Open the database from the users filesystem 
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 

    //setup the SQL statement and compile it for faster access 
    const char *sqlStatement = "select * from restaurant"; 
    sqlite3_stmt *compiledStatement; 

    if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 

     //loop through the results and add them to the feeds array 
     while (sqlite3_step(compiledStatement) == SQLITE_ROW) { 

      //read the data from the result row 
      NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
      NSString *aAddress = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
      NSString *aCity = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; 
      NSString *aProvinceState = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)]; 
      NSString *aPostalZipCode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)]; 
      NSString *aCountry = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)]; 
      NSString *aPhoneNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)]; 
      NSString *aHours = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)]; 
      double aLat = sqlite3_column_double(compiledStatement, 9); 
      double aLon = sqlite3_column_double(compiledStatement, 10); 

      CLLocation *secondLocation = [[[CLLocation alloc] initWithLatitude:aLat longitude:aLon] autorelease]; 
      CLLocationDistance restDistance = [secondLocation distanceFromLocation:firstLocation]; 

      CLLocationDistance distanceKm = restDistance/1000.0; 

      NSString *aDist = [[NSString alloc] initWithFormat: @"%f", distanceKm]; 

      Restaurant *restaurant = [[Restaurant alloc] initWithName:aName address:aAddress city:aCity provinceState:aProvinceState postalZipCode:aPostalZipCode country:aCountry phoneNumber:aPhoneNumber hours:aHours latitude:aLat longitude:aLon distance:aDist]; 

      //add the restaurant object to the restaurant array 
      [restaurants addObject:restaurant]; 

      [restaurant release]; 

     } 


    } 

    sqlite3_finalize(compiledStatement); 

} 

sqlite3_close(database); 

} 

一旦這個功能已經完成它的過程中,餐館陣列應該是排序包含數組餐廳的對象是在最近的(最小距離雙值)以最遠(最大距離雙倍值)。

+1

你怎麼弄的對象放入數組並不重要,所以這是真的就如同[如何一個NSMutableArray與它的自定義對象進行排序?](http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray -with-custom-objects-it-it) – 2010-12-15 01:12:02

回答

3

使用NSMutableArray的sortUsingSelector在您的Restaurant對象上調用比較函數。

@implementation Restaurant 

- (double) distance { return _distance; } 

- (NSComparisonResult) compareDistance:(Restaurant *) iRHS { 
    double      lhs, rhs; 

    lhs = [self distance]; 
    rhs = [iRHS distance]; 

    if (lhs < rhs) return NSOrderedAscending; 
    if (lhs > rhs) return NSOrderedDescending; 

    return NSOrderedSame; 
} 


@end 

... 

[restaurants sortUsingSelector: @selector(compareDistance:)]; 
+0

非常感謝您的及時回覆。我非常感謝您對解決方案的幫助! – syedfa 2010-12-19 18:36:24

相關問題