posts - 186,  comments - 17,  views - 35万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

   [ZOYSessionManager dataWithUrlString:GetVideoDataComment andParameter:@{@"id":userID,@"pageNo":@1,@"pageSize":@20}  SucessBlock:^(id data) {

        

//        NSDictionary * dic = [(NSData *)data  objectFromJSONData];

        

        self->_mode=[CommunityModel mj_objectWithKeyValues:data];

                NSLog(@"mode%@",self->_mode);

        for (self->_itemmodel in self->_mode.content) {

        [self.accountArray addObject:self->_itemmodel];

            NSMutableArray *timeArr = [[NSMutableArray alloc]init];

            NSMutableDictionary *dateKeyArr = [[NSMutableDictionary alloc]init];

            for(CommunitItemModel *acc in _accountArray) {

                // 时间戳转成时间对象用于排序

                int timer = [acc.date intValue];

                NSDate  *date = [NSDate dateWithTimeIntervalSince1970:timer];

                [timeArr addObject:date];

                // 时间戳转成时间戳字符串作为key,制作哈希表

                NSNumber *dataNum = [NSNumber numberWithLongLong:timer];

                NSString *datekey = [dataNum stringValue];

                [dateKeyArr setObject:acc forKey:datekey];

            }

            // 3.将时间NSDate数组排序

            NSArray *orderedDateArray = [timeArr sortedArrayUsingComparator:^NSComparisonResult(NSDate *date1, NSDate *date2) {

                // 降序排序,最近的时间靠前

                return [date2 compare:date1];

            }];

            

            // 4.根据排序好的时间数组对号入座将对象按时间排序

            // 临时数组,保存排序后的对象数组

            NSMutableArray *sortedAccounts = [[NSMutableArray alloc]init];

            NSDate *datekey = [[NSDate alloc]init];

            for (int i = 0; i<orderedDateArray.count; i++) {

                datekey = orderedDateArray[i];

                // 日期对象转换成时间戳字符串key

                NSString *datekeys = [NSString stringWithFormat:@"%lld", (long long)[datekey timeIntervalSince1970]];

                // 根据时间戳字符串key取对应的对象(哈希表)

                [sortedAccounts addObject:[dateKeyArr objectForKey:datekeys]];

            }

            // 5.更新排序后的对象数组[ARC中不需要手动释放排序前的数组]

            _accountArray = sortedAccounts;

            

            self.dataArray = _accountArray;

//            [self.dataArray addObject:self->_itemmodel];

            [self.myTableView reloadData];

            

        }

        

//                NSLog(@"dataArray%@",self.dataArray);

          [self.myTableView reloadData];

    } failureBlock:^(NSError *error) {

        NSLog(@"error  is  %@",error);

    }];

    NSLog(@"userID%@,@1,@20",userID);

    self.myTableView.estimatedRowHeight = 80.0f;

    self.myTableView.rowHeight = UITableViewAutomaticDimension;

//    [self.myTableView reloadData];

}

 整合后的小demohttps://github.com/ZOYOOPlus/TimeSortDemo

posted on   ZOYOO  阅读(772)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示