每个项目都有一个 NSDictionary 的 NSMutableArray 非常好。你可以排序。
NSMutableArray *rows = @[].mutableCopy;
[rows addObject:@{@"name": @"Bob", @"url": [NSURL URLWithString:@"http://bob.com"]}];
[rows addObject:@{@"name": @"Alice", @"url": [NSURL URLWithString:@"http://alice.net"]}];
NSLog(@"%@", rows);
[rows sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[obj1 valueForKey:@"name"] compare:[obj2 valueForKey:@"name"] options:NSNumericSearch];
}];
NSLog(@"%@", rows);
输出:
2013-07-20 00:33:31.682 MyApp[28496:303] (
{
name = Bob;
url = "http://bob.com";
},
{
name = Alice;
url = "http://alice.net";
}
)
2013-07-20 00:33:31.682 MyApp[28496:303] (
{
name = Alice;
url = "http://alice.net";
},
{
name = Bob;
url = "http://bob.com";
}
)