2012-08-05 103 views

回答

18
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"thisIsTheNameOfYourDateProperty" ascending:NO]; 
NSArray *orderedArray = [arrayOfCustomObjects sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
8

试试这个:

NSArray* newArray = [array sortedArrayUsingComparator: ^NSComparisonResult(MyClass *c1, MyClass *c2) 
{ 
    NSDate *d1 = c1.date; 
    NSDate *d2 = c2.date; 

    return [d1 compare:d2]; 
}]; 

也有一些方法来排序代替可变数组类似于以上。

编辑:我知道还有其他方法可以使用较少的代码来做到这一点,但我确实更喜欢各种事物的块枚举器,所以我发现它在实践中保持有用。你也可以调整这些以使用更多的非标准排序(整数与对象),并使用NSLog()...实际记录发生的情况。

相关问题