1

我已经用冒泡排序完成了日期排序。但是,在测试进行到一半时,我注意到它没有对最后一行进行排序。任何人都可以帮忙吗?:( 这对我来说非常重要,因为它是我需要为排序代码做的基础:

NSInteger count = [sortingArray count];
        for(int a = 0 ; a < count; a++) {
            for(int b = a+1 ; b < count; b++) {
            NSDictionary *dic = [items objectAtIndex:a];
            NSDictionary *dic2 = [items objectAtIndex:b];
            NSString *dateString = [dic valueForKey:@"date"];
            NSString *dateString2 = [dic2 valueForKey:@"date"];
            //NSLog(@"DATE: %@", dateString);   //date is the string representation.

            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            [dateFormatter setDateFormat:@"dd/MM/yyyy"];
            NSDate *endDate = [dateFormatter dateFromString:dateString];
            NSDate *endDate2 = [dateFormatter dateFromString:dateString2];

            NSDate *currDate = [NSDate date];
            NSString *startDateString = [dateFormatter stringFromDate:currDate];
            NSDate *startDate = [dateFormatter dateFromString:startDateString];
            [dateFormatter release];

            NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            NSDateComponents *comp = [gregorianCalendar components: NSDayCalendarUnit 
                                                          fromDate:startDate 
                                                            toDate:endDate
                                                           options:0];


            NSDateComponents *comp2 = [gregorianCalendar components: NSDayCalendarUnit 
                                                           fromDate:startDate
                                                             toDate:endDate2
                                                            options:0];
            [gregorianCalendar release];

            NSInteger compareDay = [comp day];    
            NSInteger compareDay2 = [comp2 day];  
            NSString *dayString = [NSString stringWithFormat:@"%d", compareDay];
                NSString *dayString2 = [NSString stringWithFormat:@"%d", compareDay2];
            NSLog(@"string 1%@", dayString); 
                NSLog(@"string 2 ----%@", dayString2); 

            if (compareDay > compareDay2){
                [sortingArray exchangeObjectAtIndex:a withObjectAtIndex:b];
            } 
        }    //b loop
    }   //a loop
        NSInteger counterIndex = [sortingArray count];
        for(int c = 0; c < counterIndex ; c++) {
            [temp addObject:[sortingArray objectAtIndex:c]];
        }        

现在看起来如何请帮助很多很多很多谢谢!

4

2 回答 2

1

使用您的初始 for 循环,如下所示:

    for(int a = 0 ; a < count-1; a++) { // Allow uptil second last position in array
        for(int b = a+1 ; b < count; b++) {  // Allow uptil last position in array

我想这就是错过的。试试这个。希望这对你有用。

于 2012-07-25T16:18:45.850 回答
1

最后我还是使用了 NSSortDescriptor 进行排序。最初我使用它,但在显示器上什么都没有出现,因此我创建了自己的大量代码来进行日期排序。无论如何我使用的方法:

NSLog(@"Before sorting: %@", temp);  // What I have is a NSMutableArray called temp
    NSSortDescriptor *dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"date" ascending: YES];

    NSArray *sortedArray = [temp sortedArrayUsingDescriptors:[NSArray arrayWithObject: dateSortDescriptor]];
    NSLog(@"After Sorting: %@", sortedArray);        
    NSMutableArray *afterSorting = [[(NSArray*)sortedArray mutableCopy]autorelease];

我所做的只是将 NSArray 转换回 NSMutableArray,然后再将其插入我的表中 :) 希望这可能对其他人有所帮助,并感谢帮助我的人^^

于 2012-07-26T14:42:44.887 回答