iOS使用冒泡排序进行排序,但最后一行未排序

时间:2012-07-25 15:58:11

标签: iphone ios sorting ios4 loops

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

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

how it looks like for now请帮助许多人多多谢谢!!

2 个答案:

答案 0 :(得分: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

我想这就错过了。 试试吧。希望这对你有用。

答案 1 :(得分: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,然后将其插入到我的表中:)希望这可能对其他人有所帮助,并感谢帮助我的人^^