Я извлекаю строки из таблицы SQLite, затем динамически создаю объект и затем добавляю объект в NSMutableArray.Моя проблема заключается в том, что я не могу найти способ динамической сортировки объектов в массиве на основе атрибута distance (который является двойным), поскольку я добавляю каждый объект в NSMutableArray в цикле for, от наименьшего доСамый большой.
Мой соответствующий код выглядит следующим образом:
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);
}
Как только эта функция завершит свою работу, массив ресторанов должен быть отсортированным массивом, содержащим объекты ресторана в порядке ближайшего (наименьшее значение двойного расстояния) до самого дальнего (наибольшее значение двойного расстояния).