按日期排序的对象,数组中的时间问题(Iphone开发)

时间:2011-07-23 07:31:12

标签: iphone objective-c xcode ios4 iphone-sdk-3.0

我遇到与数组排序有关的问题。

我有一个NSMutable数组说是A.它在每个索引上都有一个类对象b。

class b包含多个字段,如int,string和nsdate。

我希望以b类时间(NSdate)为基础对A数组进行排序。

我遵循stackoverflow上的日期排序问题,但这仅适用于日期数组。

Sort NSArray of date strings or objects

请指导我。

提前感谢

2 个答案:

答案 0 :(得分:1)

在这里,您只需根据需求修改部分代码

- (NSArray *)sortedWeightEntriesByWeightDate:(NSArray *)unsortedArray {

    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:0];

    @try {
        for(int i = 0; i < [unsortedArray count];i++) {

            NSDateFormatter *df = [[NSDateFormatter alloc]init];
            MyDataModal *entry = [unsortedArray objectAtIndex:i];       
            [df setDateFormat:@"yyyy-MM-dd"];       
            NSDate *date = [df dateFromString:entry.weightDate];        
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];

            if(date) {  
                [dict setObject:entry forKey:@"entity"];        
                [dict setObject:date forKey:@"date"];       
                [tempArray addObject:dict];
            }
            [df release];
        }

        NSInteger counter = [tempArray count];
        NSDate *compareDate;
        NSInteger index;

        for(int i = 0 ; i < counter; i++) {
            index = i;
            compareDate = [[tempArray objectAtIndex:i] valueForKey:@"date"];        
            NSDate *compareDateSecond;

            for(int j = i+1 ; j < counter; j++) {
                compareDateSecond=[[tempArray objectAtIndex:j] valueForKey:@"date"];
                NSComparisonResult result = [compareDate compare:compareDateSecond];
                if(result == NSOrderedDescending) {
                    compareDate = compareDateSecond;
                    index=j;
                }
            }
            if(i!=index)
                [tempArray exchangeObjectAtIndex:i withObjectAtIndex:index];
        }


        NSInteger counterIndex = [tempArray count];
        for(int i = 0; i < counterIndex ; i++) {
            [sortedArray addObject:[[tempArray objectAtIndex:i] valueForKey:@"entity"]];
        }

    }
    @catch (NSException * e) {
        NSLog(@"An exception occured while sorting weight entries by date");
    }
    @finally {
        return [NSArray arrayWithArray:sortedArray];
    }   
}

答案 1 :(得分:1)

怎么样:

NSArray *myArray = [NSArray arrayWithObjects:
                    [NSDictionary dictionaryWithObjectsAndKeys:[NSDate distantFuture], @"theDate", nil],
                    [NSDictionary dictionaryWithObjectsAndKeys:[NSDate distantPast], @"theDate", nil],
                    nil];
NSLog(@"Before sorting: %@", myArray);
NSSortDescriptor *dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"theDate" ascending: YES];

NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:[NSArray arrayWithObject: dateSortDescriptor]];
NSLog(@"After Sorting: %@", sortedArray);

这假设您要排序的日期有一个键(它本质上是一个属性。)