【问题标题】:How to get asccending order item by date?如何按日期获取升序项目?
【发布时间】:2015-06-10 10:03:25
【问题描述】:

我有一本包含许多项目的字典。我想按日期排序升序项目。我的日期格式是:10-Jun-2015 09:27:11

我试过这段代码但没有用:aux 是我的带有日期组件的数组。

for (int i=0; i<aux.count-1; i++) {
            for (int j=i+1; j<aux.count; j++) {
                NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
                [dateFormat setDateFormat:@"dd-MM-yyyy hh:mm:ss"];
                NSDate *date1 = [dateFormat dateFromString:[aux objectAtIndex:i]];
                NSDate *date2 = [dateFormat dateFromString:[aux objectAtIndex:j]];

                NSComparisonResult comparisonResult = [date1 compare:date2];
                if (comparisonResult==-1) {
                    NSString *var= [aux objectAtIndex:i];
                    [aux replaceObjectAtIndex:i withObject:[aux objectAtIndex:j]];
                    [aux replaceObjectAtIndex:j withObject:var];

                }
            }
        }

我想比较两个日期(date1,date2)。如果 date2 像 date1 一样大,我想交换。我想手动进行交换:如果 date2 更大,我只想从方法中获取 true1

【问题讨论】:

  • 您正在使用冒泡排序,这非常慢。使用正确的 Cocoa 方法。您还创建了大约 count^2 / 2 NSDateFormatters,这又非常慢。还有“但没用”:你的意思是什么?
  • 我不能使用可可方法。如果我使用可可方法,他只订购带有日期组件的数组,但我需要按日期订购所有字典项目。所以我需要知道改变的位置
  • 字典是无序的。您可以对数组进行排序,但不能对字典进行排序。
  • 我知道。因此,当我在数组中与日期组件 objectatindex:1 和 objectatindex:2 互换时,我将更改所有项目。我的字典由许多数组组成。
  • 你检查我的答案了吗

标签: ios objective-c date


【解决方案1】:

NSMutableArray 使用其排序方法之一处理排序(例如sortUsingComparator:):

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm:ss"];

[aux sortUsingComparator:^NSComparisonResult(NSString *datestring1, NSString *datestring2) {
    NSDate *date1 = [dateFormatter dateFromString:datestring1];
    NSDate *date2 = [dateFormatter dateFromString:datestring2];
    return [date1 compare:date2];
}];

旁注:您还应该考虑将日期字符串转换为NSDates 一次,并将它们作为NSDates 存储在您的数组中。

【讨论】:

  • 如果条件类似,我该如何使用它?
  • 你现在明白了吗?
  • 不是很好。 2014 年 10 月 6 日 12:48:07、2015 年 6 月 10 日 00:00:00、2015 年 6 月 10 日 03:08:00、2015 年 6 月 10 日 03:09:00、2015 年 6 月 10 日 03 :09:00, 2015 年 6 月 10 日 07:46:00, 2015 年 6 月 10 日 08:00:00, 2015 年 6 月 10 日 09:27:11, 2015 年 6 月 10 日 10:00:00, 09 -2015 年 6 月 18:20:00、2015 年 6 月 9 日 17:20:00、2015 年 6 月 9 日 17:14:00、2015 年 6 月 9 日 16:14:00、2014 年 10 月 20 日 13: 36:53、2014 年 10 月 18 日 16:23:26、2014 年 10 月 17 日 17:00:51、2014 年 10 月 12 日 12:57:55、2014 年 10 月 14 日 12:08:33、14- 2014 年 10 月 12:31:06、2014 年 10 月 3 日 16:31:06、2015 年 6 月 10 日 00:00:00、2015 年 6 月 10 日 07:25:48、2015 年 6 月 10 日 08:18 :06, 10-Jun-2015 09:23:49, 10-Jun-2015 11:02:28, 那是退回你的鳕鱼。
  • 再次更新dd-MMM-yyyy HH:mm:ss 注意大写HH 24 小时制。已确认与您的列表合作。
【解决方案2】:

假设您有一个 NSMutableArray 对象,其中包含 NSDate 类型的字段“startDate”,然后使用以下代码:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:YES];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

【讨论】:

    【解决方案3】:

    尝试比较并填充如下所示

        NSComparisonResult result;
        result = [Str_StoreOpening compare:Str_StoreClosing]; // comparing two dates
    
        NSString *Str_CloseTime;
        if(result==NSOrderedAscending){
            //populate in Ordered Ascending in here
    
        }else if(result==NSOrderedDescending){
            //populate in Ordered Descending in here
    
    
        }else{
            //NSLog(@"Both dates are same");
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多