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

问题描述 投票:1回答:2

我想对一个带有日期的数组进行排序,但日期只对日期和月份进行排序,而不是按年份排序我的代码:

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];
}];

它没有按日期排序。

ios objective-c nsdateformatter
2个回答
0
投票

这似乎是一个错字。

请替换代码

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

0
投票

问题是d1你使用obj1d2你也使用obj1。所以只是拉尔克里希纳写的一个错字。这是固定版本:

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];
}];
© www.soinside.com 2019 - 2024. All rights reserved.