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
我正在对NSMutableArray中的日期字符串进行排序,我正在从sqlite db中获取数组。如果你打印数组,它会像这样显示
date strings are ( "2011-05-01", "2011-02-01", "2012-01-08", "2012-05-08", "2010-01-09 )
日期字符串为(“2011-05-01”、“2011-02-01”、“2012-01-08”、“2012-05-08”、“2010-01-09)
I want to show the dates in ascending order. Please help me out guys..... I'm newbie to objc..
我想按升序显示日期。请帮帮我,伙计们……我新手objc . .
3 个解决方案
#1
22
First you should convert all date Strings (which is NSString
) objects to NSDate
objects and then sort these dateObjects
.
首先,您应该将所有日期字符串(即NSString)对象转换为NSDate对象,然后对这些数据对象进行排序。
I believe you have dateArray
containing all those strings.
我相信你有dateArray包含所有这些字符串。
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];
}];
#2
5
If your dates are really strings in the format YYYY-mm-dd
, as in your question, then this will sort them in ascending order:
如果你的日期是yyyyy -mm-dd格式的字符串,如你的问题,那么这将按升序对它们进行排序:
[arrayOfDates sortUsingSelector:@selector(compare:)];
That will also work if your dates are actually NSDate
objects.
如果您的日期实际上是NSDate对象,那么也可以这样做。
If you want to create a sorted copy of the array:
如果要创建数组的已排序副本:
NSArray *sortedArray = [arrayOfDates sortedArrayUsingSelector:@selector(compare:)];
#3
1
Sorting should be done, imo, by the database, in general. sqlite3 does support order by.
一般来说,排序应该通过数据库来完成。sqlite3确实支持order by。
#1
22
First you should convert all date Strings (which is NSString
) objects to NSDate
objects and then sort these dateObjects
.
首先,您应该将所有日期字符串(即NSString)对象转换为NSDate对象,然后对这些数据对象进行排序。
I believe you have dateArray
containing all those strings.
我相信你有dateArray包含所有这些字符串。
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];
}];
#2
5
If your dates are really strings in the format YYYY-mm-dd
, as in your question, then this will sort them in ascending order:
如果你的日期是yyyyy -mm-dd格式的字符串,如你的问题,那么这将按升序对它们进行排序:
[arrayOfDates sortUsingSelector:@selector(compare:)];
That will also work if your dates are actually NSDate
objects.
如果您的日期实际上是NSDate对象,那么也可以这样做。
If you want to create a sorted copy of the array:
如果要创建数组的已排序副本:
NSArray *sortedArray = [arrayOfDates sortedArrayUsingSelector:@selector(compare:)];
#3
1
Sorting should be done, imo, by the database, in general. sqlite3 does support order by.
一般来说,排序应该通过数据库来完成。sqlite3确实支持order by。