如何在 Objective-C 中动态排序 NSMutableArray?

发布于 2024-10-07 19:30:45 字数 2997 浏览 7 评论 0原文

我从 SQLite 表中检索行,然后动态创建一个对象,然后将该对象添加到 NSMutableArray 中。我的问题是,我无法找到一种根据距离属性(双精度)对数组中的对象进行动态排序的方法,因为我在 for 循环中将每个对象添加到 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);

}

一旦该函数完成其进程,餐馆数组应该是一个包含餐馆对象的排序数组,这些餐馆对象按最近(最小距离双值)到最远(最大距离双值)的顺序排列。

I am retrieving rows from SQLite table, then dynamically creating an object, and then adding the object to an NSMutableArray. My problem is that I am unable to figure out a way to dynamically sort the objects in the array based on the distance attribute (which is a double), as I am adding each object to the NSMutableArray in a for-loop, from smallest to largest.

My relevant code looks like this:

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);

}

Once this function has finished its course, the restaurants array should be a sorted array containing restaurant objects that are in order of nearest (smallest distance double value) to furthest (largest distance double value).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一瞬间的火花 2024-10-14 19:30:45

使用 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:)];

Use NSMutableArray's sortUsingSelector to call a comparison function on your Restaurant object.

@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:)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文