日期数组排序问题,iPhone sdk

发布于 2024-12-06 15:31:20 字数 703 浏览 0 评论 0原文

我有一个包含不同日期值的数组。我使用以下代码对日期数组进行排序,完成。

    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的两个部分中。我怎样才能实现它?请帮我找到解决方案。

I have an array that contains different date values. And I have used the following code to sort the date array, its done.

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

Now the result is in the combinedArr, I need to check the each value with current system time and need to load into two different arrays, and load these two arrays into two sections of a tableView. How can I implement that? Please help me to find a solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过度放纵 2024-12-13 15:31:20

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

像这样:

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

I think that the simplest and the fastest (shorter running time) solution is to create 2 separate arrays from the beginning and sort each one separately.

Like this:

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文