zoukankan      html  css  js  c++  java
  • iOS数组排序 请求后,数组元素的排序 时间戳,最热,点赞数等

       [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

  • 相关阅读:
    Vue 获取URL链接后面的参数值
    Vue 跳转到指定页面,返回到上一页
    Vant 插件
    Vue 返回上一页,记住上一页的数据
    vue pc端支付宝支付
    Spring 中的事务
    数据库中锁与事务
    《产品方法论》 读书笔记
    设计模式之装饰者模式
    设计模式之单例模式
  • 原文地址:https://www.cnblogs.com/tangyuanby2/p/6767385.html
Copyright © 2011-2022 走看看