对 UITableView 进行降序排序

Posted

技术标签:

【中文标题】对 UITableView 进行降序排序【英文标题】:Sort UITableView descending order 【发布时间】:2014-04-22 20:03:52 【问题描述】:
- (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?

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

【问题讨论】:

您已就该主题提出的前两个问题的答案有什么问题? @rmaddy 这对我有用,但我也被困在这部分。 Best way to sort an NSArray of NSDictionary objects?的可能重复 “这是一个表格视图”是什么意思?您不是在尝试对 UITableView 进行排序,而是在尝试对 NSArray 进行排序。重复的大卫指出你做同样的事情:排序一个 NSArray。 【参考方案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]];

【讨论】:

它对我不起作用,也许我没有以正确的方式实现它。能否请您准确一点。基于上面我自己的代码的示例代码将非常有帮助。 我猜“问题”是combindedStrings 不是属性,所以代码不会为他/她编译。 OP 可能应该阅读基本(Objective-C)编程。【参考方案2】:

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];
                    ];

这里的“客户端”是自定义对象,数据存储在属性中以便于访问。

希望对你有帮助

【讨论】:

以上是关于对 UITableView 进行降序排序的主要内容,如果未能解决你的问题,请参考以下文章

按降序对数组进行排序的更好方法

Firebase 按降序对数据进行排序。 (只排序了一半。)

按降序对单链表进行排序

如何按降序对列表视图项进行排序

如何在Ruby中按降序对数组进行排序

UITableView 以排序顺序划分,未正确划分