日期数组排序问题,iPhone sdk



我有一个包含不同日期值的数组。我已经使用下面的代码排序日期数组,它完成了。

    combinedArr = [[NSMutableArray alloc]init];
NSInteger counts = [pbTitle count];
for (int i = 0; i < counts; i++) {
    CustomObject *customobject2 = [CustomObject customObjectWithName:
                                   [pbTitle objectAtIndex:i] andDate:[pbstartDate objectAtIndex:i]];
    [combinedArr addObject:customobject2];
}
[combinedArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
 {
     return [[(CustomObject*)obj1 date]compare: [(CustomObject*)obj2 date]];
 }];
NSLog(@"Results: %@", combinedArr);

现在结果在combinedArr中,我需要用当前系统时间检查每个值并需要加载到两个不同的数组中,并将这两个数组加载到tableView的两个部分中。我该如何实现呢?

我认为最简单和最快(更短的运行时间)的解决方案是从一开始就创建两个单独的数组,并分别对每个数组进行排序。

:

NSDate *currentDate = [NSDate date];
NSMutableArray *pastArray = [[NSMutableArray alloc] init];
NSMutableArray *futureArray = [[NSMutableArray alloc] init];
NSInteger counts = [pbTitle count];
// Fill the arrays
for (int i = 0; i < counts; i++) {
    NSDate *customOnjectDate = [pbstartDate objectAtIndex:i];
    CustomObject *customobject2 = [CustomObject customObjectWithName:[pbTitle objectAtIndex:i] andDate:customOnjectDate];
    NSMutableArray *array = ([customOnjectDate compare:currentDate] == NSOrderedAscending ? pastArray : futureArray);
    [array addObject:customobject2];
}
// Sort the arrays
[pastArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
     return [[obj1 date] compare:[obj2 date]];
 }];
[futureArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
     return [[obj1 date] compare:[obj2 date]];
 }];
// Use the arrays
NSLog(@"pastArray: %@", pastArray);
NSLog(@"futureArray: %@", futureArray);
// Don't forget to release the arrays after you use them
[pastArray release];
[futureArray release];

相关内容

  • 没有找到相关文章

最新更新