iOS 根據時間排序

2021-08-02 10:15:20 字數 2487 閱讀 5796

這個是回答這篇文章的問題

建立個date模型

#import 

@inte***ce

date : nsobject

@property (nonatomic, copy) nsstring *time;

@end

之後賦值進行排序

date *d1 = [[date alloc] init];

d1.time = @"2012-12-02";

date *d2 = [[date alloc] init];

d2.time = @"2412-12-02";

date *d3 = [[date alloc] init];

d3.time = @"2312-12-02";

date *d4 = [[date alloc] init];

d4.time = @"2312-12-02";

date *d5 = [[date alloc] init];

d5.time = @"2812-08-02";

date *d6 = [[date alloc] init];

d6.time = @"0000-09-02";

_dataarray = [nsmutablearray array];

[_dataarray addobject:d1];

[_dataarray addobject:d2];

[_dataarray addobject:d3];

[_dataarray addobject:d4];

[_dataarray addobject:d5];

[_dataarray addobject:d6];

for (int i = 0; i < _dataarray.count; i ++)

nslog(@"\n");

nssortdescriptor *sorter = [[nssortdescriptor alloc] initwithkey:@"time" ascending:yes];

nsmutablearray *sortdescriptors = [[nsmutablearray alloc] initwithobjects:&sorter count:1];

nsarray *sortarray = [_dataarray sortedarrayusingdescriptors:sortdescriptors];

for (int j = 0; j < sortarray.count; j ++)

2017-06-10

19:59:35.046

timesort[4740:678220] 排序前 ==

2012-12-02

2017-06-10

19:59:35.047

timesort[4740:678220] 排序前 ==

2412-12-02

2017-06-10

19:59:35.047

timesort[4740:678220] 排序前 ==

2312-12-02

2017-06-10

19:59:35.047

timesort[4740:678220] 排序前 ==

2312-12-02

2017-06-10

19:59:35.047

timesort[4740:678220] 排序前 ==

2812-08-02

2017-06-10

19:59:35.047

timesort[4740:678220] 排序前 ==

0000-09-02

2017-06-10

19:59:35.048

timesort[4740:678220]

2017-06-10

19:59:35.048

timesort[4740:678220] 排序後 ==

0000-09-02

2017-06-10

19:59:35.048

timesort[4740:678220] 排序後 ==

2012-12-02

2017-06-10

19:59:35.048

timesort[4740:678220] 排序後 ==

2312-12-02

2017-06-10

19:59:35.048

timesort[4740:678220] 排序後 ==

2312-12-02

2017-06-10

19:59:35.048

timesort[4740:678220] 排序後 ==

2412-12-02

2017-06-10

19:59:35.049

timesort[4740:678220] 排序後 ==

2812-08-02

iOS 根據字典key排序提取value

字典本身無序,我們有時候需要依據有序字典key提取value。獲取字典有序value陣列 對傳入的字典key進行排序,依序提取value入陣列 param dict nsdictionary return nsarray nsarray ordervaluearraywithdictionary n...

ios 對時間排序

排序 對時間進行排序 nsmutablearray dataarray nsmutablearray alloc initwithcapacity 0 nsmutabledictionary dir nsmutabledictionary alloc init dir setobject 2012 ...

swift 對json陣列根據時間排序

通過伺服器獲取到乙個json陣列,有可能陣列裡的資料時亂序的,當有需求要按照時間的順序顯示陣列中的model,便可利用swift中的sorted函式來進行排序。如下 按日期排序 self.datadayarr dataarray.sorted by ocmpare函式解析 其中 q ordereda...