ios - Objective C 如何在 array 中按升序排序日期

  显示原文与译文双语对照的内容
123 1

我有一个包含字符串格式的日期的NSMutableArray 。在这里,我需要按照日期的升序排序那个 array 。我用了的strong

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

但它只按日期和月份的升序排序日期。不考虑年份。请帮助我。例如 array 包含

 03/09/2017, 03/06/2016, 01/06/2016,01/04/2016 and 03/01/2017.

在使用上面的代码行之后,array 包含

 01/04/2018, 01/06/2016, 03/01/2017, 03/06/2016, 03/09/2016
时间:原作者:0个回答

101 4

你需要像这样使用 sortedArrayUsingComparator 以字符串 array 排序日期。

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSArray *sortedArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
 NSDate *d1 = [df dateFromString: obj1];
 NSDate *d2 = [df dateFromString: obj2];
 return [d1 compare: d2];
}];

注意:如果你的日期是 formate,那么很难从你的例子中找到日期,这就是我用 MM/dd/yyyy的原因,如果你的日期包含了 formate,那就用它。

原作者:
144 0

你的代码是正确的但日期格式错误。

请尝试以下代码:

NSMutableArray *dateArray = [[NSMutableArray alloc] initWithArray: @[@"03/09/2017", @"03/06/2016", @"01/06/2016",@"01/04/2016", @"03/01/2017"]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *date;
for (int i = 0; i <dateArray.count; i++) {
 [formatter setDateFormat:@"MM/dd/yyyy"];//Please your date format set.
 date = [formatter dateFromString:[dateArray objectAtIndex:i]];
 [formatter setDateFormat:@"yyyy/MM/dd"];
 [dateArray replaceObjectAtIndex:i withObject:[formatter stringFromDate:date]];
}
NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[dateArray sortedArrayUsingDescriptors:descriptors];
NSLog(@"Array: %@",reverseOrder);
原作者:
68 5

请尝试以下代码:

NSArray *strDateArray = [[NSArray alloc] initWithObjects:@"03/09/2017",@"03/06/2016", @"01/06/2016",@"01/04/2016",@"03/01/2017", nil];
 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 [formatter setDateFormat:@"MM/dd/yyyy"];
 NSMutableArray *dateArray = [[NSMutableArray alloc] init];
 for (NSString *dateString in strDateArray) {
 NSDate *date = [formatter dateFromString:dateString];
 [dateArray addObject:date];
 }
//sort array of dates
 [dateArray sortUsingComparator:^NSComparisonResult(NSDate *date1, NSDate *date2) {
//return date1 compare date2 for ascending. And reverse the call for ascending.
 return [date1 compare:date2];
 }];
 NSLog(@"dateArray %@", dateArray);
 NSMutableArray *sortedDateArray = [[NSMutableArray alloc] init];
 for (NSDate *date in dateArray) {
 NSString *dateString = [formatter stringFromDate:date];
 [sortedDateArray addObject:dateString];
 }
 NSLog(@"sortedDateArray %@", sortedDateArray);
原作者:
118 5

用于 Swift 4.0

let formater = DateFormatter()
 formater.dateFormat ="MM/dd/yyyy"//date formate should be according to your date formate
 let sortedArray=arrayToSort.sorted(){
 (obj1, obj2) in
 let date1=formater.date(from: obj1)
 let date2=formater.date(from: obj2)
 return date1!<date2!
 }
原作者:
93 0

//You 可以根据你的代码更改日期格式

NSDateFormatter *df = [[NSDateFormatter alloc] init] ;[ df setDateFormat:@"MM/dd/yyyy"];

 NSArray *sortedArray = [YourArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2)
 {
 NSString *strDateObj1 = [NSString stringWithFormat:@"%@",[obj1 valueForKey:@"BirthDate"]];
 NSString *strDateObj2 = [NSString stringWithFormat:@"%@",[obj2 valueForKey:@"BirthDate"]];
 NSDate *d1 = [df dateFromString: strDateObj1];
 NSDate *d2 = [df dateFromString: strDateObj2];
 return [d1 compare: d2];
 }];

NSLog ( @"%@",sortedArray

原作者:
...