2010-11-30 90 views
1

根据日期排序数组, 问题是以字符串格式保存的日期可以在NSDate对象中对数组进行排序而不更改日期(核心数据)。根据日期排序数组

串格式是毫米/日/年

+0

什么是字符串格式 - 举一个例子! – deanWombourne 2010-11-30 12:41:18

+0

这些数据是存储在数组中的普通对象还是它在Core Data图中的管理对象? – TechZen 2010-11-30 20:23:56

+0

它是核心数据实体,也是通过使用访问日期字符串的唯一方法。运算符与核心数据实体的对象。 – Ishu 2010-12-01 04:18:07

回答

1
-(NSMutableArray *)sortByStringDate:(NSMutableArray *)unsortedArray 
{ 
    NSMutableArray *tempArray=[NSMutableArray array]; 
    for(int i=0;i<[unsortedArray count];i++) 
    { 

     NSDateFormatter *df=[[NSDateFormatter alloc]init]; 
     objModel=[unsortedArray objectAtIndex:i]; 

     [df setDateFormat:@"MM/dd/yyyy"]; 
     NSDate *date=[df dateFromString:objModel.sDate]; 
     NSMutableDictionary *dict=[NSMutableDictionary dictionary]; 
     [dict setObject:objModel forKey:@"entity"]; 
     [dict setObject:date forKey:@"date"]; 
     [tempArray addObject:dict]; 
    } 

    NSInteger counter=[tempArray count]; 
    NSDate *compareDate; 
    NSInteger index; 
    for(int i=0;i<counter;i++) 
    { 
     index=i; 
     compareDate=[[tempArray objectAtIndex:i] valueForKey:@"date"]; 
     NSDate *compareDateSecond; 
     for(int j=i+1;j<counter;j++) 
     { 
      compareDateSecond=[[tempArray objectAtIndex:j] valueForKey:@"date"]; 
      NSComparisonResult result = [compareDate compare:compareDateSecond]; 
      if(result == NSOrderedAscending) 
      { 
       compareDate=compareDateSecond; 
       index=j; 
      } 
     } 
     if(i!=index) 
      [tempArray exchangeObjectAtIndex:i withObjectAtIndex:index]; 
    } 


    [unsortedArray removeAllObjects]; 
    for(int i=0;i<[tempArray count];i++) 
    { 
     [unshortedArray addObject:[[tempArray objectAtIndex:i] valueForKey:@"entity"]]; 
    } 
    return unsortedArray; 

} 

这对我有用。

0

如果日期被存储在格式,其中的字母排序是相当于一个日期排序,那么它不能与核心数据来完成。在这种情况下,您有两种选择:

  1. 将新的NSDate属性添加到您的数据库并将每个文本日期转换为实际日期并更新记录。然后您可以对核心数据进行排序
  2. 将所有数据提取到内存数组中,然后使用自定义比较方法对其进行排序。
1

这里要走的路是使用NSArray方法-sortedArrayUsingFunction:context:。这适用于iOS 2.0及更高版本,非常简单。你会打电话:

sortedArray = [unsortedArray sortedArrayUsingFunction:sortByStringDate context:NULL]; 

你还需要定义像这样的功能:

NSInteger sortByDateString(id string1, id string2, void *context) 
{ 
    int y1 = [[string1 yearComponent] intValue]; 
    int y2 = [[string2 yearComponent] intValue]; 
    if (y1 < y2) 
     return NSOrderedAscending; 
    else if (v1 > v2) 
     return NSOrderedDescending; 
    else 
     int m1 = [[string1 monthComponent] intValue]; 
     int m2 = [[string2 monthComponent] intValue]; 
     if (m1 < m2) 
      return NSOrderedAscending; 
    // et cetera 
} 

你需要自己的逻辑分离出来的年,月,和字符串的日期这样你可以比较每一个。我不确定这是否比按照丹尼斯的建议将字符串转换为日期的工作要少,但是您知道自己的程序也适用于那些可以工作的程序。

0
NSMutableArray* scores; 


scores = [NSMutableArray arrayWithArray:[defaults 
objectForKey:@"HighScores"]]; 
[scores addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Andrew", @"Name", [NSNumber numberWithUnsignedInt:45], @"Score", nil]]; 
[scores addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Andrew2", @"Name", [NSNumber numberWithUnsignedInt:55], @"Score", nil]]; 
[scores sortUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"Score" ascending:NO] autorelease]]]; 
0

将NSMutableArray排序为日期列,您有任何格式日期此代码工作正常。

NSSortDescriptor *sorter =[[NSSortDescriptor alloc] initWithKey:@"Date column name" ascending:sortCheck selector:@selector(compare:)]; 
        [itemsData sortUsingDescriptors:[NSArray arrayWithObject:sorter]]; 
0

这是我的解决方案。

NSComparisonResult dateSort(NSString *s1, NSString *s2, void *context) { 

[[DateFormatter sharedDateFormatter]setDateStyle:NSDateFormatterShortStyle]; 
NSDate *d1 = [[DateFormatter sharedDateFormatter] dateFromString:s1]; 
NSDate *d2 = [[DateFormatter sharedDateFormatter] dateFromString:s2]; 
return [d1 compare:d2]; 

}

这需要的时间,这是目前的字符串,将它转换为一个日期,然后进行比较。 sharedDateFormatter是NSDateFormatter的单例类,所以我不必每次都分配它。

虽然我不会在项目中使用它,但会挂起主线程,但它可能对某人有所帮助。如果有人知道一个更好的方法来做到这一点,这不会阻止主线程,身份证欣赏它。