我有一个数组,其中包含一个表示为字符串的日期列表:
NSMutableArray *objArray=[[NSMutableArray alloc]init];
[objArray addObject:@"18-01-2013 2:51"];
[objArray addObject:@"16-01-2013 5:31"];
[objArray addObject:@"15-01-2013 3:51"];
[objArray addObject:@"17-01-2013 4:41"];
[objArray addObject:@"03-02-2013 3:21"];
[objArray addObject:@"05-01-2013 3:01"];
请告诉我如何使用日期按升序排列此数组。
答案 0 :(得分:3)
NSDateFormatter * formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@“dd-MM-yyyy hh:mm”];
NSComparator compareDates = ^(id string1, id string2)
{
NSDate *date1 = [formatter dateFromString:string1];
NSDate *date2 = [formatter dateFromString:string2];
return [date1 compare:date2];
};
NSSortDescriptor * sortDesc = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO comparator:compareDates]autorelease];
[objArray sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
通过使用此代码,我得到了确切的输出,谢谢你的所有
答案 1 :(得分:3)
我们可以采用另一种方式来获取带日期的排序数组
NSArray *objArr = [[NSArray alloc]initWithObjects:@"2003/08/02 03:00 ",@"2001/02/04 04:00 ",@"2001/02/04 05:00 ",@"2010/05/01 08:00 ",@"2002/12/02 02:00 ",@"2012/11/05 02:00 ",@"2013/10/01 12:00 ", nil];
NSArray *sortedArray = [objArr sortedArrayUsingComparator:^(id firstObject, id secondObject) {
return [(NSDate * )firstObject compare: (NSDate * ) secondObject];
}];
NSLog(@"sorteed Array %@",sortedArray);
答案 2 :(得分:0)
NSSortDescriptor * descLastname = [[NSSortDescriptor alloc] initWithKey:@"active" ascending:YES];
[livevideoparsingarray sortUsingDescriptors:[NSArray arrayWithObjects:descLastname, nil]];
[descLastname release];
videoparsing = [livevideoparsingarray copy];
livevideoparsingarray是我的排列和排序active是我的标签,它是在我排序的数组中。你随着你的要求改变了。
答案 3 :(得分:0)
您可以使用NSSortDescriptor
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
return [objArray sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
答案 4 :(得分:0)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *str1 = @"18-01-2013";
NSString *str2 = @"16-01-2013";
NSString *str3 = @"15-01-2013";
NSString *str4 = @"17-01-2013";
NSString *str5 = @"03-02-2013";
NSString *str5 = @"05-01-2013";
NSArray *arr = [NSArray arrayWithObjects:str1, str2, str3,str4,str5 nil];
arr = [arr sortedArrayUsingFunction:dateSort context:nil];
NSLog(@"New dates %@",arr);
}
//The date sort function
NSComparisonResult dateSort(NSString *s1, NSString *s2, void *context) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy"];
NSDate *d1 = [formatter dateFromString:s1];
NSDate *d2 = [formatter dateFromString:s2];
return [d1 compare:d2]; // ascending order
// return [d2 compare:d1]; // descending order
}
答案 5 :(得分:0)
试试这个
NSMutableArray *objArray;
objArray=[[NSMutableArray alloc] init];
[objArray addObject:@"18-01-2013 2:51"];
[objArray addObject:@"16-01-2013 5:31"];
[objArray addObject:@"15-01-2013 3:51"];
[objArray addObject:@"17-01-2013 4:41"];
[objArray addObject:@"03-02-2013 3:21"];
[objArray addObject:@"05-01-2013 3:01"];
NSSortDescriptor *sort_Date=[[NSSortDescriptor alloc] initWithKey:@"self"ascending:NO];
NSLog(@"%@",[[objArray sortedArrayUsingDescriptors:@[sort_Date]] description]);
我收到了以下回复
2013-02-06 11:23:35.100[1135:c07]
(
"18-01-2013 2:51",
"17-01-2013 4:41",
"16-01-2013 5:31",
"15-01-2013 3:51",
"05-01-2013 3:01",
"03-02-2013 3:21"
)