对 NSDate 进行排序
我的程序中有几个字典,我已将所有这些字典放入一个数组中,我想对这些日期进行相应排序,字典应该交换其位置,下面显示的是我正在使用的代码,但我没有对其进行排序。请帮助我解决这个问题
**NSLog(@"排序前---%d",[allDataArray count]);
for(int i=0;i<[allDataArray count]-1;i++){
NSString *dateStr1=[[allDataArray objectAtIndex:i]objectForKey:@"Date"];
for(int j=i+1;j<[allDataArray count];j++){
NSString *dateStr2=[[allDataArray objectAtIndex:j]objectForKey:@"Date"];
if(([(NSDate*)dateStr1 compare:(NSDate*)dateStr2]==NSOrderedAscending)
||([(NSDate*)dateStr1 compare:(NSDate*)dateStr2]==NSOrderedSame))
[allDataArray replaceObjectAtIndex:i withObject:[allDataArray objectAtIndex:j]];
}
}
NSLog(@"afterSorting-numbers--%@",allDataArray);**
i have several dictionaries in my program,i have put all those dictionaries in an array i want to sort those dates accordingly dictionary should exchange its position,below shown is the code i am using,but i am not getting it sorted.Please help me to fix this issue
**NSLog(@"before sorting---%d",[allDataArray count]);
for(int i=0;i<[allDataArray count]-1;i++){
NSString *dateStr1=[[allDataArray objectAtIndex:i]objectForKey:@"Date"];
for(int j=i+1;j<[allDataArray count];j++){
NSString *dateStr2=[[allDataArray objectAtIndex:j]objectForKey:@"Date"];
if(([(NSDate*)dateStr1 compare:(NSDate*)dateStr2]==NSOrderedAscending)
||([(NSDate*)dateStr1 compare:(NSDate*)dateStr2]==NSOrderedSame))
[allDataArray replaceObjectAtIndex:i withObject:[allDataArray objectAtIndex:j]];
}
}
NSLog(@"afterSorting-numbers--%@",allDataArray);**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来你正在重新发明轮子(排序已经在几十年前解决),改为这样做:
或者这样:
Looks like you're reinventing the wheel here (sorting has been solves decades ago), do this instead:
or this: