[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

iOS数组排序 请求后,数组元素的排序 时间戳,最热,点赞数等的更多相关文章

  1. 对数组元素进行排序的方法总结(利用C++)

    首先,对数组元素进行排序方法总结为以下两类: 一.简单排序算法(时间复杂度O(n*n)) 1.插入排序 2.选择排序 3.交换排序,即冒泡排序 二.先进排序算法(时间复杂度O(n*logn)) 1.快 ...

  2. c - 向一个排序好的数组插入一个数,插入后数组依然是排序好的

    概述 因为这里只是演示插入1个数,这里我不用malloc再重新分配,而是将原先数组的腾出一个占位符. 完整代码如下: #include <stdio.h> #define LEN 6 // ...

  3. C语言:通过指针对数组元素进行排序

    // //  main.c //  Pointer_array // //  Created by ma c on 15/8/2. //  Copyright (c) 2015年 bjsxt. All ...

  4. js将数组元素随机排序的方法

    在群里看见的一个面试题,试了一下,还是可以做出来的,但是需要查资料,主要是岁一些方法了解的不清楚,可能这个跟我平时不太注重基础理论有关系,像什么构造函数啊,我根本就不关心什么叫构造函数,我一直都以为我 ...

  5. php数组的各种排序

    转自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/10/11/2207494.html 如果你已经使用了一段时间PHP的话,那么,你应 ...

  6. 数组Array.sort()排序的方法

    数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...

  7. shell脚本之数组基本操作及排序

    数组的基本操作及排序 1.数组定义方法: ( 6 7 9 4 3 2) 0 1 2 3 4 5 #下标号 方法一: 数组名=(value0 value1 value2 -) 方法二: 数组名=([0] ...

  8. [Swift]LeetCode462. 最少移动次数使数组元素相等 II | Minimum Moves to Equal Array Elements II

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  9. 462. 最少移动次数使数组元素相等 II

    给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000. 例如: 输入: [1,2,3] 输出: 2 说明: 只 ...

随机推荐

  1. Idea 切换git账号

    重置一下账号设置,再次执行拉取或推送会提示重新输入账号密码 进入项目根目录执行:git config --system --unset credential.helper

  2. Spring中Bean生命周期

    Spring中的bean生命周期是一个重要的点,只有理解Bean的生命周期,在开发中会对你理解代码是非常有用的.对于Bean的周期,个人认为可以分为四个阶段.第一阶段:Bean的实例化,在该阶段主要是 ...

  3. 转:java 重定向和转发的区别

    response.sendredirect("http://www.foo.com/path/error.html"); 重定向和转发有一个重要的不同:当使用转发时,JSP容器将使 ...

  4. 【Semantic segmentation】Fully Convolutional Networks for Semantic Segmentation 论文解析

    目录 0. 论文链接 1. 概述 2. Adapting classifiers for dense prediction 3. upsampling 3.1 Shift-and-stitch 3.2 ...

  5. LA 3971 组装电脑(二分)

    https://vjudge.net/problem/UVALive-3971 题意:你有b块钱,想要组装一台电脑.给出n个配件各自的种类.品质因子和价格,要求每种类型的配件各买一个,总价格不超过b, ...

  6. maven 本地仓库的配置

    <?xml version="1.0" encoding="UTF-8"?> <!--Licensed to the Apache Softw ...

  7. 返回值过长时被nginx截断的解决办法

    今天在写接口时碰到了这个问题,返回json格式的数据,但是被截断了经过排查,才发现是数据过大超出缓冲区最大容量,而将数据写入临时文件时又没有权限,所以再返回时,超出缓冲区的数据将丢失解决方法:给fas ...

  8. 设计模式——迭代器(Iterator)模式

    概述 迭代器模式简单的说(按我目前的理解)就是一个类提供一个对外迭代的接口,方面调用者迭代.这个迭代接口至少包括两个方法:hasNext()--用于判断是否还有下一个,next()--用于取出下一个对 ...

  9. 在Jupyter notebook中使用特定虚拟环境中的python的kernel

        在虚拟环境tf中安装完tensorflow后,在虚拟环境tf打开的jupyter里发现只有一个kernel-python3,新建一个文件, import tensorflow as tf ,发 ...

  10. python脚本7_打印九九乘法表

    #打印九九乘法表 for i in range(1,10): s = "" for j in range(1,i+1): s += str(j) + '*' + str(i) + ...