简体   繁体   English

如何用日期时间值对NSArray进行排序?

[英]How to sort NSArray with date time values?

I have an NSArray containing date/time NSStrings in the following format: 我有一个包含以下格式的日期/时间NSStringsNSArray

2/2/2011 2:46:39 PM
2/4/2011 11:59:47 AM
…

where the date is represented as month/day/year. 日期以月/日/年表示。

How do I sort this NSArray making sure the newest date/times are at the top? 如何对这个NSArray进行排序,以确保最新的日期/时间在顶部?

When you're dealing with dates, use NSDate instead of NSString . 在处理日期时,请使用NSDate而不是NSString Also, it's important to consider the time zone — does the Web service provide dates in UTC or some other time zone? 另外,考虑时区也很重要-Web服务是否以UTC或其他时区提供日期?

You should first convert your array of strings into an array of dates. 您应该首先将字符串数组转换为日期数组。 Otherwise, you'd be converting a string to a date whenever it is used for comparison, and there will be more comparisons than the number of strings. 否则,每当用于比较时,您都会将字符串转换为日期,并且进行的比较将超过字符串的数量。

For example: 例如:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/DD/YYYY hh:mm:ss a"];

NSMutableArray *dateArray = [NSMutableArray array];
for (NSString *dateString in array) {
    NSDate *date = [formatter dateFromString:dateString];
    if (date) [dateArray addObject:date];
    // If the date is nil, the string wasn't a valid date.
    // You could add some error reporting in that case.
}

This converts array , an array of NSStrings , to dateArray , a mutable array of NSDates . 这会将arrayNSStrings的数组)转换为dateArray (可变的NSDates数组)。 The date formatter uses the system time zone. 日期格式器使用系统时区。 If you want to use UTC as the time zone: 如果要将UTC用作时区:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/DD/YYYY hh:mm:ss a"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

Having done that, sorting the array is trivial: 完成此操作后,对数组进行排序很简单:

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

Use method compare to compare two dates, 使用比较方法比较两个日期,

Sample NSDate comparision, 样本NSDate比较,

NSDate *dateOne = [NSDate dateWithString:@"2008-12-04 03:00:00 +0900"];
NSDate *dateTwo = [NSDate dateWithString:@"2008-12-04 04:00:00 +0900"];

switch ([dateOne compare:dateTwo]){
case NSOrderedAscending:
NSLog(@”NSOrderedAscending”);
break;
case NSOrderedSame:
NSLog(@”NSOrderedSame”);
break;
case NSOrderedDescending:
NSLog(@”NSOrderedDescending”);
break;
}

Use you own logic to sort 使用自己的逻辑进行排序

use this 用这个

    NSMutableArray *mutArr=[yourArray mutableCopy];//convert your NSArray into mutable array
    NSDateFormatter *df=[[[NSDateFormatter alloc] init] autorealese];
    [df setDateFormat:@"MM/DD/YYYY hh:mm:ss a"];
    NSDate *compareDate;
NSInteger index;
for(int i=0;i<[mutArray count];i++)
{
    index=i;
    compareDate=[df dateFromString:[mutArray objectAtIndex:i]];
    NSDate *compareDateSecond;
    for(int j=i+1;j<counter;j++)
    {
        compareDateSecond=[df dateFromString:[mutArr objectAtIndex:j]];
        NSComparisonResult result = [compareDate compare:compareDateSecond];
       if(result == NSOrderedAscending)
       {
           compareDate=compareDateSecond;
           index=j;
       }
    }
    if(i!=index)
      [mutArr exchangeObjectAtIndex:i withObjectAtIndex:index];
}
    }

NSLog(@"%@",mutArr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM