【问题标题】:Sorting NSArray of objects on Date property在 Date 属性上对对象的 NSArray 进行排序
【发布时间】:2014-05-19 18:35:57
【问题描述】:

我正在尝试根据日期属性对对象的NSArray 进行排序。我要查找的顺序是最近的日期在前,以最早的日期结束。

例如 2014 年 5 月 15 日 2014 年 5 月 12 日 2014 年 2 月 20 日 25/12/2013 2013 年 10 月 11 日

我尝试了许多不同的方法,在“This”线程中的接受答案中看到。

但例如我的订单已关闭

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateToPay" ascending:YES];
            NSArray *orderedArray = [cashFlowItemsArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
            self.cashFlowItems = orderedArray;

正在返回:
2014 年 2 月 20 日
2014 年 5 月 12 日
2014 年 5 月 15 日
2013 年 10 月 11 日
2013 年 12 月 25 日

所以年份排序正确,月份和日期错误

非常感谢任何帮助。

【问题讨论】:

  • 我假设你的 Date 对象是一个字符串,而不是 NSDate?
  • 02/20/2014 & 25/12/2013 这里的日期格式是什么? "dd/MM/yyyy" 或 "MM/dd/yyyy" 。在将它们添加到 cashFlowItemsArray 之前,我认为您正在使用两种不同的格式化程序对它们进行格式化。
  • 创建 NSDate 对象并按这些对象排序。
  • @pawan 打错字了,格式是 mm/dd/yyyy
  • 我刚刚编辑以纠正这些错误

标签: ios objective-c sorting nsarray


【解决方案1】:

检查一下,我已经测试了你的代码

 NSArray *stringDateArray = @[@"05/15/2014",@"05/12/2014",@"02/20/2014",@"05/12/2013",@"10/11/2013"];

 NSMutableArray *dateArray = [NSMutableArray arrayWithCapacity:[stringDateArray count]];

for (NSString *dateString in stringDateArray) {

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

    [dateArray addObject:@{@"date":[dateFormatter dateFromString:dateString]}];
}

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *orderedArray = [dateArray sortedArrayUsingDescriptors:@[sortDescriptor]];

NSLog(@"ordered array--%@",orderedArray);

有序数组--( { 日期 = "2013-05-11 18:30:00 +0000"; }, { 日期 = "2013-10-10 18:30:00 +0000"; }, { 日期 = "2014-02-19 18:30:00 +0000"; }, { 日期 = "2014-05-11 18:30:00 +0000"; }, { 日期 = "2014-05-14 18:30:00 +0000"; } )

带有比较选择器

NSArray *stringDateArray = @[@"05/15/2014",@"05/12/2014",@"02/20/2014",@"05/12/2013",@"10/11/2013"];

NSMutableArray *dateArray = [NSMutableArray arrayWithCapacity:[stringDateArray count]];

for (NSString *dateString in stringDateArray) {

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

    [dateArray addObject:[dateFormatter dateFromString:dateString]];
}

NSArray *orderedArray = [dateArray sortedArrayUsingSelector:@selector(compare:)];

NSLog(@"ordered array--%@",orderedArray);

有序数组--( "2013-05-11 18:30:00 +0000", "2013-10-10 18:30:00 +0000", "2014-02-19 18:30:00 +0000", "2014-05-11 18:30:00 +0000", “2014-05-14 18:30:00 +0000” )

【讨论】:

  • 我的数组是一个对象数组,每个对象都有一个日期属性。我正在尝试订购该属性
  • 请在这里纠正我,你有一个带日期属性的数组吗?意味着在您的数组内部,您有一个带有键 @"date" & object @"NSDate" rihgt 的字典?
  • 我有一个数组,数组中填充了相同类型的对象,每个对象都有一个date属性,date属性的数据类型是一个NSdate
  • 你能告诉我你如何填写你的 cashFlowItemsArray 的代码吗?
  • 数据从 API 调用返回,在响应中我将 Json 数据解析为 cashflowitem 对象,然后我用这些 cashflowitem 对象填充数组。
【解决方案2】:

可能重复:Sort NSArray of date strings or objects

他基本上声明(假设您的数组充满了 NSDate 对象)使用 compare: 日期选择器。这样做:

NSArray *orderedArray = [dateArray sortedArrayUsingSelector:@selector(compare:)];

他说:

这比创建 NSSortDescriptor 简单,而且简单得多 而不是编写自己的比较函数。 (NSDate 对象知道如何 至少尽可能有效地相互比较 希望用自定义代码来完成。)

【讨论】:

  • 我使用@selector(compare:)得到完全相同的结果
  • 你复制我的代码是不是因为我弄错了(我现在修好了)。
【解决方案3】:

在 for: 中使用此代码将您的数组变成 NSDate 数组:

NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MM/dd/yyyy"]; NSDate *myDate = [df dateFromString: @"02/20/2014"];

然后:

[dateArray sortUsingSelector: @selector(compare:)];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 2011-09-09
    • 2012-04-26
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 2015-06-20
    相关资源
    最近更新 更多