Date array sorting issue, iPhone sdk
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 wit开发者_JAVA技巧h 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.
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];
精彩评论