迭代块中的数组以对其成员进行排序

时间:2013-03-10 19:12:53

标签: ios objective-c sorting

如何在像这样的块内的[index]处获取对象?这是为了得到它的第一个字母,并按字典顺序排序NSDictionaries数组。

NSArray *sortedArray;
sortedArray = [_photos sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSString *first = _photos[0][FLICKR_PHOTO_TITLE];
    NSString *second = _photos[0][FLICKR_PHOTO_TITLE];
    return [first compare:second];
}];
_photos = sortedArray;

1 个答案:

答案 0 :(得分:2)

看来你有一系列词典。要对FLICKR_PHOTO_TITLE键的值进行排序,请执行以下操作:

_photos = [_photos sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
    NSString *first = a[FLICKR_PHOTO_TITLE];
    NSString *second = b[FLICKR_PHOTO_TITLE];
    return [first compare:second];
}];