Iphone iOS排序采用冒泡排序,但最后一行未排序
我已经用气泡排序完成了日期排序。然而,在测试进行到一半时,我注意到它并没有对最后一行进行排序。有人能帮忙吗(这对我来说非常重要,因为这是我需要进行分类代码的基础:Iphone iOS排序采用冒泡排序,但最后一行未排序,iphone,ios,sorting,ios4,loops,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 =
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]];
}
NSInteger count=[sortingArray count];
对于(int a=0;a比较日期2){
[排序数组交换对象索引:a与对象索引:b];
}
}//b循环
}//循环
NSInteger计数器索引=[sortingArray count];
for(int c=0;c
请帮助许多人谢谢!!使用您的首字母来表示以下循环:
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
for(int a=0;a
我想这就是我错过的。
试试看。希望这对你有用。最后,我还是使用了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,然后再将其插入到表中:)希望这能帮助其他人,感谢那些帮助我的人^ ^ /p>哦,天哪!我已经做了很长时间了。我非常感谢。我尽我最大的努力来帮助你完成IOS,Objul-C,OOP,C,C++技术。我很高兴能帮助你。)对不起,我又需要帮助了。我添加了另一个值,最后一行没有再次排序!知道原因吗?:(再次卡住!!不,应该可以,可能还有其他问题。尝试在数组中仅使用2个值,然后最终将数据添加到数组中。