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
iOS数组排序 请求后,数组元素的排序 时间戳,最热,点赞数等的更多相关文章
- 对数组元素进行排序的方法总结(利用C++)
首先,对数组元素进行排序方法总结为以下两类: 一.简单排序算法(时间复杂度O(n*n)) 1.插入排序 2.选择排序 3.交换排序,即冒泡排序 二.先进排序算法(时间复杂度O(n*logn)) 1.快 ...
- c - 向一个排序好的数组插入一个数,插入后数组依然是排序好的
概述 因为这里只是演示插入1个数,这里我不用malloc再重新分配,而是将原先数组的腾出一个占位符. 完整代码如下: #include <stdio.h> #define LEN 6 // ...
- C语言:通过指针对数组元素进行排序
// // main.c // Pointer_array // // Created by ma c on 15/8/2. // Copyright (c) 2015年 bjsxt. All ...
- js将数组元素随机排序的方法
在群里看见的一个面试题,试了一下,还是可以做出来的,但是需要查资料,主要是岁一些方法了解的不清楚,可能这个跟我平时不太注重基础理论有关系,像什么构造函数啊,我根本就不关心什么叫构造函数,我一直都以为我 ...
- php数组的各种排序
转自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/10/11/2207494.html 如果你已经使用了一段时间PHP的话,那么,你应 ...
- 数组Array.sort()排序的方法
数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...
- shell脚本之数组基本操作及排序
数组的基本操作及排序 1.数组定义方法: ( 6 7 9 4 3 2) 0 1 2 3 4 5 #下标号 方法一: 数组名=(value0 value1 value2 -) 方法二: 数组名=([0] ...
- [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 ...
- 462. 最少移动次数使数组元素相等 II
给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000. 例如: 输入: [1,2,3] 输出: 2 说明: 只 ...
随机推荐
- 在Linux终端管理文件你要知道的11个命令
LS - 列表文件 ls命令列出目录中的文件. 默认情况下,使用ls列出当前目录下的文件. 2 你也可以列出文件递归-也就是说,列出所有文件在当前目录中的目录-使用ls -R.LS还可以列出在其他目录 ...
- Jenkins 集成Ansible教程
前提条件: 1. 部署Jenkins Server 2. 部署 Ansible Server 一.在Jenkins安装SSH插件 系统管理 -> 插件管理 二.在Jenkins 凭证中添加Ans ...
- [洛谷P4886]快递员
题目大意:一个$n$个点的树,树上有$m$个点对$(a,b)$,找到一个点$x$,使得$max(dis(x,a_i)+dis(x,b_i))$最小 如果做过幻想乡的战略游戏这道题,应该这道题的思路一眼 ...
- 使用淘宝的npm代理下载模块
npm install node-sass --registry=http://registry.npm.taobao.org
- webservice使用注解修改WSDL内容
首先看我们没有修改前的WSDL内容 此时服务端的类 修改后的 注解如下 package com.xiaostudy; import javax.jws.WebMethod; import javax. ...
- POJ 3352 Road Construction(边—双连通分量)
http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...
- asp.net core开发注意事项
1.类库的创建尽量选择.net standard. 如果选择.net core 则.net framework不能调用该类库, .net core和.net framework都可以调用.net st ...
- JAVA初学者(一)
2015-12-15 21:26:17 刚学的java 做个总结: 1.构造函数没有返回值. 2.A对象调用Q的方法,Q方法里的变量就是A的变量 Fraction add(Fraction f) 在 ...
- 在Jupyter notebook中使用特定虚拟环境中的python的kernel
在虚拟环境tf中安装完tensorflow后,在虚拟环境tf打开的jupyter里发现只有一个kernel-python3,新建一个文件, import tensorflow as tf ,发 ...
- js从一个select选择数据添加到另一个select(包括移除)
一.实现效果 二.要求 1.选中左侧的菜单,点击“>>”,该菜单(1项或多项选中的)将添加到右侧菜单 2.选中右侧菜单,点击“<<”,则移除选中的菜单 3.点击“>> ...