按UITableView降序排序

时间:2014-04-22 20:03:53

标签: ios objective-c uitableview sorting

- (NSArray *)combinedStrings {
return [[self.numberOfUsers arrayByAddingObjectsFromArray:self.numberOfModerators]    arrayByAddingObjectsFromArray:self.numberOfAdmins];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  NSDictionary *jsonForJam = [self.combinedStrings objectAtIndex:indexPath.row];
  return cell;
  }

我想按照降序对tableView中显示的结果进行排序,我该怎么办呢?

我已经尝试过使用它,但没有帮助:Best way to sort an NSArray of NSDictionary objects?

基于我提供的示例代码的解决方案将非常有用。

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码对字符串数组进行排序。

self.combinedStrings = [[[self.combinedStrings sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] reverseObjectEnumerator] allObjects];

NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(localizedCompare:)];
self.combinedStrings = [self.combinedStrings sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

答案 1 :(得分:0)

A)您可以实现自己的自定义排序方法(最终仍然使用iOS排序方法),因为NSDictionary使问题变得复杂。

B)创建一个对象(其目的是保存字典中的所有信息)然后将自定义对象存储在数组中,然后按照这个或任何你想要的方式对其进行排序:

self.combinedStrings = [self.combinedStrings sortedArrayUsingComparator:^(id obj1, id obj2)
                    {
                        Client *client1 = (Client*)obj1;
                        Client *client2 = (Client*)obj2;
                        if([client1.firstName isEqualToString:client2.firstName])
                        return [client1.lastName compare:client2.lastName];
                        else
                        return [client1.firstName compare:client2.firstName];
                    }];

此处“客户”将是自定义对象,数据存储在属性中以便于访问。

希望这有帮助

相关问题