0

我有一个具有不同属性的类,例如地名,位置距离,用户名,。我将这个类对象存储在一个数组中。我想根据距离对这个对象数组进行排序。我该怎么做。请用一些代码提出建议。

4

2 回答 2

1
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"placeDistance" ascending:TRUE];
[your array sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]
于 2012-11-07T10:27:05.617 回答
0
NSArray* array...
NSArray* sorted_array = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    YourClass* a = (YourClass*)obj1;
    YourClass* b = (YourClass*)obj2;
    /*
    return NSComparisonResult based on a property comparison here,
    for example, return [a.distance compare:b.distance] if distance is an NSNumber
    */
}];

很像忽略标点符号的排序(Objective-C)

于 2012-11-07T10:29:04.517 回答