我已经用冒泡排序完成了日期排序。但是,在测试进行到一半时,我注意到它没有对最后一行进行排序。任何人都可以帮忙吗?:( 这对我来说非常重要,因为它是我需要为排序代码做的基础:
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]];
}
现在看起来如何请帮助很多很多很多谢谢!