iphone sdk排序问题
遇到一些问题,我对其进行了排序,但看起来像这样,
"10/16/2011 12:00:00 AM",
"10/16/2011 12:00:00 AM",
"11/16/2011 12:00:00 AM",
"11/16/2011 12:00:00 AM",
"9/15/2011 12:00:00 AM",
"9/15/2011 12:00:00 AM",
"9/15/2011 12:00:00 AM"
格式是“MM/dd/yyyy”,我想根据日、月、年、时间对其进行排序。我怎样才能实现它?
这是代码
NSSortDescriptor* nameSorter = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
NSMutableArray *temp = [dateStart mutableCopy];
[temp sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
dateStart = temp;
NSLog(@"Sorted: %@",dateStart);
Having some problem, I got it sorted but looks likes this,
"10/16/2011 12:00:00 AM",
"10/16/2011 12:00:00 AM",
"11/16/2011 12:00:00 AM",
"11/16/2011 12:00:00 AM",
"9/15/2011 12:00:00 AM",
"9/15/2011 12:00:00 AM",
"9/15/2011 12:00:00 AM"
the format is "MM/dd/yyyy", I want to sort this based on day, then month, then year then time. How can I implement that?
Here is the code
NSSortDescriptor* nameSorter = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
NSMutableArray *temp = [dateStart mutableCopy];
[temp sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
dateStart = temp;
NSLog(@"Sorted: %@",dateStart);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
NSDateFormatter
将字符串转换为NSDate
并使用 Comparator 进行比较,输出:
You can use
NSDateFormatter
to convert the string intoNSDate
and Comparator to compare them,Output: