gpt4 book ai didi

ios - 如何按 id 或月份 : Descending (Latest Post First)? 对数组进行排序

转载 作者:行者123 更新时间:2023-11-28 21:03:56 25 4
gpt4 key购买 nike

我有一个 UITableView 和一个 UICollectionView 里面。我想将其排序为下降(最新帖子优先)。我该怎么做?我尝试了几种方法,但都不起作用。

以下是我的代码和响应

  [Utilities serverRequest:@{@"getAnnouncement":@"a6dba37437ced2c3b07469fd6c0661f3"} completionBlock:^(id response) {

collectionViewDic[@"announcement"] = response[@"response"];
_annArray = response[@"response"];
NSLog(@"AnnouncementResponse%@",response);

// sorting to newest
/*
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"anc_id" ascending:NO];
NSMutableArray *sortDescriptors = [NSMutableArray arrayWithObject:sortDescriptor];
[_annArray sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"Sorted - AnnouncementResponse%@",response);
*/
/*
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"anc_id" ascending:NO];
NSArray* sortedArray = [_annArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSLog(@"Sorted Array Response -%@",sortedArray);
*/
announcmentDone = YES;
if (newsDone && bulletInDone && announcmentDone && !refreshed) {
refreshed = YES;
[table reloadData];
}
} errorBlock:nil];

Response

最佳答案

_annArray 实际上是一个 NSDictionary,键为“2017 年 9 月”、“2017 年 8 月”等……据我所知,每个键的值都是一个包含单个 NSDictionary 的数组。要获得您需要的所有值,例如:

    NSArray *responseValues = [response[@"response"] allValues]; // An NSArray of NSArrays
NSMutableArray *dictionarys = [NSMutableArray new];
for (NSArray *dictArrays in responseValues) {
for (NSDictionary *dict in dictArrays) {
[dictionarys addObject:dict];
}
}
_annArray = dictionarys;

然后您应该能够像您期望的那样进行排序:

  NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"anc_id" ascending:NO];
NSArray* sortedArray = [_annArray sortedArrayUsingDescriptors:@[sortDescriptor]];
NSLog(@"Sorted Array Response -%@", sortedArray);

不过,这是一种快速、粗略的方法。在实践中,最好将每个 JSON block 解析为一个对象,并进行类型检查和对象排序。

关于ios - 如何按 id 或月份 : Descending (Latest Post First)? 对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46790526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com