简体   繁体   English

在目标c中用日期月份和年份对日期数组进行排序

[英]Sort array of dates with date month and year in objective c

I want to sort an array with a date but the date has only sort with date and month, and not sorting with year my code: 我想用日期对数组进行排序,但是日期只用日期和月进行排序,而不用年份对我的代码进行排序:

NSArray *sortedArray = [arrClassSehedule sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
    NSDate *d1 = [appSharedData.CommanFormatForConditional dateFromString: [obj1 objectForKey:@"scheduleStart"]];
    NSDate *d2 = [appSharedData.CommanFormatForConditional dateFromString: [obj1 objectForKey:@"scheduleStart"]];
    return [d1 compare: d2];
}];

It's not sorting properly by date. 按日期排序不正确。

The problem is for d1 you are using obj1 and for d2 you also use obj1 . 问题是对于d1您使用的是obj1 ;对于d2您也使用的是obj1 So just a typo as Lal Krishna wrote. 所以就像Lal Krishna所说的错字。 Here's the fixed version: 这是固定版本:

NSArray *sortedArray = [arrClassSehedule sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
    NSDate *d1 = [appSharedData.CommanFormatForConditional dateFromString:[obj1 objectForKey:@"scheduleStart"]];
    NSDate *d2 = [appSharedData.CommanFormatForConditional dateFromString:[obj2 objectForKey:@"scheduleStart"]];
    return [d1 compare:d2];
}];

It seems like a typo. 好像是错字。

Please replace the code with 请替换为

 NSDate *d2 = [appSharedData.CommanFormatForConditional dateFromString: [obj2 objectForKey:@"scheduleStart"]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM