如何在ios中按升序排序日期数组

时间:2016-09-03 08:36:44

标签: ios objective-c nsdate

我有一个NSMutableArray,其中包含字符串格式的日期。在这里,我需要按日期的升序排序该数组。我使用了强文

 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

使用上面的代码行后,Array包含like,

adapter: mysql2
encoding: utf8
pool: 5 
username: root  
password:_*****

5 个答案:

答案 0 :(得分:6)

您需要像这样使用sortedArrayUsingComparator来使用String数组对日期进行排序。

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

注意:设置日期的格式根据您的日期,如果您的日期包含格式{{1},则很难从您的示例中预测日期格式,这就是我使用MM/dd/yyyy的原因然后使用它。

答案 1 :(得分:1)

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

试试这段代码:

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);

答案 2 :(得分:1)

请尝试以下代码:

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);

答案 3 :(得分:0)

对于Swift 4.0

box-shadow

答案 4 :(得分:0)

//You can change the date format according to your code

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

NSArray *sortedArray = [YourArraysortedArrayUsingComparator:^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)