如何对日期对象的NSMutableArray进行排序

如何对日期对象的NSMutableArray进行排序

本文介绍了如何对日期对象的NSMutableArray进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些关于在NSMutableArray中排序日期字符串的小工作,我从sqlite db获取数组。
如果您打印数组它会显示如下

I'm doing some small work on sorting the date strings in the NSMutableArray, i'm getting the array from the sqlite db.If you print the array it is showing like this

日期字符串是(
2011-05-01,
2011-02-01,
2012-01-08,
2012-05-08,
2010-01-09

date strings are ( "2011-05-01", "2011-02-01", "2012-01-08", "2012-05-08", "2010-01-09)

我想按升序显示日期。请帮帮我们......我是objc的新手..

I want to show the dates in ascending order. Please help me out guys..... I'm newbie to objc..

推荐答案

首先,您应该将所有日期字符串( NSString )对象转换为 NSDate 对象然后对这些 dateObjects 进行排序。

First you should convert all date Strings (which is NSString) objects to NSDate objects and then sort these dateObjects.

我相信你有 dateArray 包含所有这些字符串。

I believe you have dateArray containing all those strings.

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self"
                                                           ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];
NSArray *reverseOrder = [dateArray sortedArrayUsingDescriptors:descriptors];

OR

NSArray *reverseOrderUsingComparator = [dateArray sortedArrayUsingComparator:
                                       ^(id obj1, id obj2) {
                                           return [obj2 compare:obj1];
                                       }];

这篇关于如何对日期对象的NSMutableArray进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:11