【发布时间】:2014-02-19 17:47:50
【问题描述】:
我有一个对象数组(字典),其中包含一个键 @"Category"。我必须按从该键开始的特定键对其进行排序。例如,如果 objects[@"Category"]: A, B ,C ,D ,E ,F ,G , H etc. 中的值并且用户选择“按 C 排序”,我需要按 [@"Category"] 将根数组重新排序为:C、D、E、F、G、H、A、B - 或 - C,A,B,D,E,F,G,H。
我希望很清楚我的目标是什么。我试过了:
// sortedPosts2 = [self.objects sortedArrayUsingComparator:^(PFObject *object1, PFObject *object2) {
// return [object1[@"Category"] compare:object2[@"Category"] options:NSOrderedAscending];
// }];
// NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:category ascending:YES];
// sortedPosts2 = [self.objects sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
}
【问题讨论】:
-
您的代码中似乎没有处理所选排序项...
-
sortDescriptorWithKey:category 其中 category 是用户选择的字符串
标签: ios objective-c arrays sorting