iphone sdk排序问题

发布于 2024-12-06 03:50:37 字数 604 浏览 0 评论 0原文

遇到一些问题,我对其进行了排序,但看起来像这样,

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-12-13 03:50:37

您可以使用 NSDateFormatter 将字符串转换为 NSDate 并使用 Comparator 进行比较,

NSArray *datesArray = @[@"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"];

NSLog(@"dates:%@", datesArray);

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy h:mm:ss a"];

NSArray *sortedDatesArray = [datesArray sortedArrayUsingComparator:^(id obj1, id obj2){

    NSDate *date1 = [dateFormatter dateFromString:obj1];
    NSDate *date2 = [dateFormatter dateFromString:obj2];

    return [date1 compare:date2];

}];

NSLog(@"%@",sortedDatesArray);

输出:

"9/15/2011 12:00:00 AM",
"9/15/2011 12:00:00 AM",
"9/15/2011 12:00:00 AM",
"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"

You can use NSDateFormatter to convert the string into NSDate and Comparator to compare them,

NSArray *datesArray = @[@"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"];

NSLog(@"dates:%@", datesArray);

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy h:mm:ss a"];

NSArray *sortedDatesArray = [datesArray sortedArrayUsingComparator:^(id obj1, id obj2){

    NSDate *date1 = [dateFormatter dateFromString:obj1];
    NSDate *date2 = [dateFormatter dateFromString:obj2];

    return [date1 compare:date2];

}];

NSLog(@"%@",sortedDatesArray);

Output:

"9/15/2011 12:00:00 AM",
"9/15/2011 12:00:00 AM",
"9/15/2011 12:00:00 AM",
"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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文