将对象按照时间戳排序,这里典型的一个例子是登录账户的排序:本地客户端可能保存了多个账户信息,在登录窗口用户可以选择已经登陆过的账户直接登录,现在的需求是要时刻让最近登陆过的账户排在前面,对于每个账户,每次登陆时都记录下当前登陆的时间,时间是一个时间戳(从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

iOS--自定义相册---对象数组按照时间戳排序的更多相关文章

  1. js对象数组多字段排序

    来源:js对象数组按照多个字段进行排序 一.数组排序 Array.sort()方法可以传入一个函数作为参数,然后依据该函数的逻辑,进行数组的排序. 一般用法:(数组元素从小大进行排序) var a = ...

  2. iOS 自定义的对象类型的解档和归档

    自定义的对象的解档和归档 如果想对自己自定义的类进行解档和归档的话 必须遵循一个协议:NSCoding Student.h 文件 #import <Foundation/Foundation.h ...

  3. JS框架设计之对象数组化一种子模块

    类数组对象是一个很好的存储结构,但是功能太弱了,为了享受纯数组的哪些便捷的方法,使用前可以做下转换,通常可以使用$.slice.call()方法做转换,但是旧版本的IE下的HTMLCollection ...

  4. JAVA(1)之关于对象数组作形参名的方法的使用

    public class Test{ int tour; public static void cs(Test a[]) { for (int i = 0; i < a.length; i++) ...

  5. iOS探索:对NSArray中自定义的对象进行排序

    http://mobile.51cto.com/hot-434804.htm 我们开发的每个程序都会使用到一些数据,而这些数据一般被封装在一个自定义的类中.例如一个音乐程序可能会有一个Song类,聊天 ...

  6. iOS数组排序 请求后,数组元素的排序 时间戳,最热,点赞数等

    [ZOYSessionManager dataWithUrlString:GetVideoDataComment andParameter:@{@"id":userID,@&quo ...

  7. iOS开发之谓词Predicate和对象数组的排序

    我们在开发中经常使用的Predicate谓词,主要是正则表达式的使用,今天给大家简单的讲讲怎样去使用谓词. 因为内容比较简单,所以直接上代码展示: NSMutableArray *people_arr ...

  8. 对象数组自定义排序--System.Collections.ArrayList.Sort()

    使用System.Collections.ArrayList.Sort()对象数组自定义排序 其核心为比较器的实现,比较器为一个类,继承了IComparer接口并实现int IComparer.Com ...

  9. iOS中怎么存储照片到自定义相册

    在市场上主流App中,大多数App都具有存储图片到自己App的相册中.苹果提供的方法只能存储图片到系统相册,下面讲一下怎么实现: 实现思路:  1.对系统相册进行操作的前提必须导入#import &l ...

随机推荐

  1. 转载 - 浅析我曾遇到的几个便宜VPS服务器

    本文来自:http://www.jianshu.com/p/7d8cfa87fa32 有些时候可能并不是我们工作和业务的需要,但是网上就是这么的邪门,如果看到便宜的衣服不去购买深怕自己吃亏.所以每年的 ...

  2. hiho一下 第四十四周 博弈游戏·Nim游戏(直接公式解)

    证明看这http://hihocoder.com/contest/hiho44/problem/1 思路: 设 sg=a[1]^a[2]^...a[n],若sg=0,则先手Alice必败,否则必赢. ...

  3. Android(java)学习笔记135:SQLite数据库(表)的创建 以及 SQLite数据库的升级

    一.数据库的创建 1.文件的创建      //引用,如果文件不存在是不会创建的   File  file = new File("haha.txt"):     //输出流写数据 ...

  4. CentOS 软RAID5

    yum install -y mdadm np1回车 tfdw mdadm --create /dev/md5 --level=5 --raid-devices=3 /dev/sdb1 /dev/sd ...

  5. xpath定位和css定位对比

    xpath定位和css定位对比   实际项目中使用较多的是xpath定位和css定位.XPath是XML文档中查找结点的语法,换句话就是通过元素的路径来查找这个元素.xpath比较强大,而css选择器 ...

  6. HTML之基本语法(表单)

    一.表单的基本介绍 表单:就是互联网上用于收集用户信息的一种结构,在HTML当中事先定义好了一种标签来完成此事,标签名称为form,它是一个双标签<form action="" ...

  7. 1_HDFS理论及安装部署

    一.hadoop简介 1.hadoop的初衷是为了解决Nutch的海量数据爬取和存储的需要,HDFS来源于google的GFS,MapReduce来源于Google的MapReduce,HBase来源 ...

  8. 如何解决U盘装系统后磁盘总容量变小?

    我在用Win32_Disk_Imager工具制作U盘系统盘之后,发现U盘大小变为2M,另外的大小没有被分配,解决办法如下. 打开:http://jingyan.baidu.com/article/59 ...

  9. 【状压dp】cf906C. Party

    需要稍加分析结论:还有一些小细节 Arseny likes to organize parties and invite people to it. However, not only friends ...

  10. NowCoder 9.9 模拟赛

    T1.中位数 二分答案x,原序列大于x的置为1,小于x的置为-1,判断是否存在长度大于m的区间和大于0(也就是大于x的数多于小于x的数),若有,则ans>=x,否则ans<x #inclu ...