2

我有一个数组,其中包含表示为字符串的日期列表:

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

请告诉我如何使用日期按升序排列这个数组。

4

6 回答 6

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);
于 2013-02-06T11:27:45.790 回答
3

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; [格式化程序 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]];

通过使用此代码,我得到了准确的输出,谢谢大家

于 2013-02-06T06:04:51.383 回答
0

您可以使用NSSortDescriptor

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
return [objArray sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
于 2013-02-06T05:17:35.643 回答
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"

)

于 2013-02-06T05:54:29.147 回答
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
}
于 2013-02-06T05:32:18.843 回答
0
NSSortDescriptor * descLastname = [[NSSortDescriptor alloc] initWithKey:@"active" ascending:YES];
[livevideoparsingarray sortUsingDescriptors:[NSArray arrayWithObjects:descLastname, nil]];
 [descLastname release];
videoparsing = [livevideoparsingarray copy];

livevideoparsingarray 是我的数组,我有排序 & active 是我的标签,它在我有排序的数组中。你随着你的要求而改变。

于 2013-02-06T05:13:27.353 回答