将对象按照时间戳排序,这里典型的一个例子是登录账户的排序:本地客户端可能保存了多个账户信息,在登录窗口用户可以选择已经登陆过的账户直接登录,现在的需求是要时刻让最近登陆过的账户排在前面,对于每个账户,每次登陆时都记录下当前登陆的时间,时间是一个时间戳(从1970年到现在的秒数)。我们要做的是将时间戳排序,然后按照时间戳的顺序将所有账户排序。当然这也适用于其他关于时间排序的问题。


实现思路和过程

  • 1.先将每个账户对象的时间戳变量(要足够精确,采用long long int)取出来:一方面要将每个时间戳转换成NSDate对象用于排序;另一方面要将每一个时间戳转换成一个字符串作为key和对应的账户对象放入字典中做成一个哈希表,用于之后根据排序好的时间戳将账户对象数组排序。

    排序过程需要一个数组用于时间排序的NSDate对象,一个字典作为存放‘时间戳-对象’的哈希表:

    // 时间戳数组(存放时间NSDate对象用于排序)
NSMutableArray *timeArr = [[NSMutableArray alloc]init];
// 时间戳-对象字典,将对象和其对应的时间戳字符串存入字典(哈希表)
NSMutableDictionary *dateKeyArr = [[NSMutableDictionary alloc]init]; // 时间戳取出,并格式化处理
for(Account *acc in _accountArray) {
// 1.时间戳转成时间对象用于排序
NSDate *date = [NSDate dateWithTimeIntervalSince1970:acc.loginTime];
[timeArr addObject:date];
// 2.时间戳转成时间戳字符串作为key,制作哈希表
NSNumber *dataNum = [NSNumber numberWithLongLong:acc.loginTime];
NSString *datekey = [dataNum stringValue];
[dateKeyArr setObject:acc forKey:datekey];
}

2.将取出的NSDate对象数组排序

    // 3.将时间NSDate数组排序
NSArray *orderedDateArray = [timeArr sortedArrayUsingComparator:^NSComparisonResult(NSDate *date1, NSDate *date2) {
// 降序排序,最近的时间靠前
return [date2 compare:date1];
}];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 3.按照排序好的时间数组,安排好的顺序将对象从哈希表一次取出得到排序好的对象数组:
    // 根据排序好的时间数组对号入座将对象按时间排序
// 临时数组,保存排序后的对象数组
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]];
} // sortedAccounts就是我们要的结果了

完整的示例Demo

这里制作一个只包含用户名和时间戳的假账户数据,排序后按照顺序显示在一个textview中: 

账户Account

//
// Account.h
// TimeSortDemo
//
// Created by Xinhou Jiang on 22/12/16.
// Copyright © 2016年 Xinhou Jiang. All rights reserved.
// #import <Foundation/Foundation.h> @interface Account : NSObject @property (nonatomic, copy) NSString *name; // 姓名
@property (nonatomic, assign) long long int loginTime; // 上次登录时间戳(距离1970年的秒数) + (Account*)newAccountWithName:(NSString *)name andTime:(long long int)logintime; @end
//
// Account.m
// TimeSortDemo
//
// Created by Xinhou Jiang on 22/12/16.
// Copyright © 2016年 Xinhou Jiang. All rights reserved.
// #import "Account.h" @implementation Account + (Account *)newAccountWithName:(NSString *)name andTime:(long long)logintime {
Account *acc = [[Account alloc] init];
acc.name = name;
acc.loginTime = logintime;
return acc;
} @end

UIViewController

//
// ViewController.m
// TimeSortDemo
//
// Created by Xinhou Jiang on 22/12/16.
// Copyright © 2016年 Xinhou Jiang. All rights reserved.
// #import "ViewController.h"
#import "Account.h" @interface ViewController () @property(nonatomic, strong) IBOutlet UITextView *text; @property (nonatomic, strong) NSMutableArray<Account*> *accountArray; // 账户数组 @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 请求数据
[self request];
// 排序前
[self showUI]; } - (void) request {
// 初始化数组并添加几个账户假数据对象
_accountArray = [[NSMutableArray alloc] init];
[_accountArray addObject:[Account newAccountWithName:@"张三" andTime:1450675000]];
[_accountArray addObject:[Account newAccountWithName:@"李四" andTime:1450923000]];
[_accountArray addObject:[Account newAccountWithName:@"小明" andTime:1450656000]];
[_accountArray addObject:[Account newAccountWithName:@"小丽" andTime:1450435000]];
} // 将数组按照时间戳排序
- (IBAction)sort:(id)sender {
/** 按照时间戳排序 **/
// 1.初始化
// 时间戳数组(存放时间NSDate对象用于排序)
NSMutableArray *timeArr = [[NSMutableArray alloc]init];
// 时间戳-对象字典,将对象和其对应的时间戳字符串存入字典(哈希表)
NSMutableDictionary *dateKeyArr = [[NSMutableDictionary alloc]init]; // 2.时间戳取出,并格式化处理
for(Account *acc in _accountArray) {
// 时间戳转成时间对象用于排序
NSDate *date = [NSDate dateWithTimeIntervalSince1970:acc.loginTime];
[timeArr addObject:date];
// 时间戳转成时间戳字符串作为key,制作哈希表
NSNumber *dataNum = [NSNumber numberWithLongLong:acc.loginTime];
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 showUI];
} // 显示数据到页面
- (void) showUI {
NSString *s = [NSString stringWithFormat:@"%@[%lld]\n%@[%lld]\n%@[%lld]\n%@[%lld]",
_accountArray[0].name,_accountArray[0].loginTime,
_accountArray[1].name,_accountArray[1].loginTime,
_accountArray[2].name,_accountArray[2].loginTime,
_accountArray[3].name,_accountArray[3].loginTime];
_text.text = s;
} @end

最新文章

  1. JavaWeb chapter11 编写无脚本的JSP页面
  2. Python 单例
  3. [原]SQL_实验2.1.3 清华大学出版社
  4. 对云风 cstring 第二次解析
  5. CSS样式一
  6. IOS 10适配https 包含对于一些http的一些兼容配置
  7. ios解析XML和json数据
  8. Winform控件缩写
  9. C#使用WebKitBrowser.dll填坑记
  10. 移动web:tab选项卡
  11. 如何嗅闻交换网络和ARP骗子-ARP解释的原则
  12. Lvs+Keepalived+Bind+web构建高可用负载均衡系统
  13. ssh免秘钥登录
  14. 理解Spring中的IOC和AOP
  15. postgres 数据库的安装
  16. php运行出现Call to undefined function curl_init()解决方法
  17. shell脚本中各类括号的作用(小结)
  18. 9、BOM (浏览器对象模型)
  19. c++实现“扫描检测硬件改动”
  20. python解析命令行参数

热门文章

  1. POJ 3311 Hie with the Pie (状压DP)
  2. HDU 4055 Number String(DP计数)
  3. Kippo-Failed to load application: &#39;module&#39; object has no attribute &#39;IPluggableAuthenticationModules&#39;
  4. FiraCode 字体 =&gt; 箭头函数变成 整体 还有 等于 不等于
  5. solver
  6. kubernetes监控-prometheus(十六)
  7. javaweb基础(20)_JavaBean总结
  8. EXC_BAD_ACCESS调试
  9. 测试 code style
  10. mysql 备份 常用脚本