如何根据特定参数对对象数组进行排序

时间:2012-11-07 10:22:31

标签: iphone objective-c

我有一个类具有不同的属性,如地名,地点距离,用户名,。我正在将此类对象存储在数组中。我想根据距离对此对象数组进行排序。我可以这样做。请指示一些代码。

2 个答案:

答案 0 :(得分:1)

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"placeDistance" ascending:TRUE];
[your array sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]

答案 1 :(得分: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
    */
}];

很像Sort ignoring punctuation (Objective-C)