对包含 2 种类型的对象的 NSmutableArray 进行排序?

发布于 2024-11-30 15:18:00 字数 161 浏览 1 评论 0原文

我有一个 NSMutableArray,其中包含 2 种类型的对象。

两个对象都包含一个具有不同名称的日期字段(具有不同格式的日期字符串)。

根据日期对它们进行排序的最佳和最快方法是什么?在这种情况下是否可以使用 NSSortDescriptor

I have an NSMutableArray that contains 2 types of Objects.

Both objects contain a field for dates (date Strings with different formats) with different names.

What's the best and fastest way to sort them all based on the date? Is it possible to use an NSSortDescriptor in this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尤怨 2024-12-07 15:18:00

如果您的目标是 Mac OS X v10.6 或 iOS4 及更高版本,您可以使用与此类似的比较器进行排序

NSDateFormatter *formatter1 = ...; //create and configure date formatter for Class1
NSDateFormatter *formatter2 = ...; //create and configure date formatter for Class2


[arrayOfObjects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSDate *date1 = nil, *date2 = nil;
    //Get first objects date
    if([obj1 isKindOfClass:[Class1 class]])
    {
        date1 = [formatter1 dateFromString:[obj1 class1Date]];
    }
    else if([obj1 isKindOfClass:[Class2 class]])
    {
        date1 = [formatter2 dateFromString:[obj1 class2Date]];
    }

    //Get second objects date
    if([obj2 isKindOfClass:[Class1 class]])
    {
        date2 = [formatter1 dateFromString:[obj2 class1Date]];
    }
    else if([obj2 isKindOfClass:[Class2 class]])
    {
        date2 = [formatter2 dateFromString:[obj2 class2Date]];
    }

    NSAssert(date1 != nil, @"Could not parse date from %@", obj1);
    NSAssert(date2 != nil, @"Could not parse date from %@", obj2);

    return [date1 compare:date2];
}];

除了检查类之外,您还可以检查它是否响应选择器。

If you are targeting Mac OS X v10.6 or iOS4 and higher you can sort using comparator similar to this

NSDateFormatter *formatter1 = ...; //create and configure date formatter for Class1
NSDateFormatter *formatter2 = ...; //create and configure date formatter for Class2


[arrayOfObjects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSDate *date1 = nil, *date2 = nil;
    //Get first objects date
    if([obj1 isKindOfClass:[Class1 class]])
    {
        date1 = [formatter1 dateFromString:[obj1 class1Date]];
    }
    else if([obj1 isKindOfClass:[Class2 class]])
    {
        date1 = [formatter2 dateFromString:[obj1 class2Date]];
    }

    //Get second objects date
    if([obj2 isKindOfClass:[Class1 class]])
    {
        date2 = [formatter1 dateFromString:[obj2 class1Date]];
    }
    else if([obj2 isKindOfClass:[Class2 class]])
    {
        date2 = [formatter2 dateFromString:[obj2 class2Date]];
    }

    NSAssert(date1 != nil, @"Could not parse date from %@", obj1);
    NSAssert(date2 != nil, @"Could not parse date from %@", obj2);

    return [date1 compare:date2];
}];

Besides checking for the class you could also check if it responds to selector.

神妖 2024-12-07 15:18:00

您可以添加一个类别来统一日期字段名称。假设 A 类的实例有一个字段“dateString”,而 B 类的实例有一个字段“creationDateString”...您可以这样做:

@implementation A (SortByDate)

-(NSDate*)sortDate
{
    static NSDateFormatter * formatter = nil ;
    static dispatch_once_t once = 0 ;
    dispatch_once( & once, ^{ formatter = ... } ) ;

    return [ formatter dateFromString:self.dateString ] ;
}
@end

@implementation B (SortByDate)
-(NSDate*)sortDate
{
    static NSDateFormatter * formatter = nil ;
    static dispatch_once_t once = 0 ;
    dispatch_once( & once, ^{ formatter = ... } ) ;

    return [ formatter dateFromString:self.creationDateString ] ;
}
@end

现在您可以按 sortDate 属性对数组中的所有对象进行排序:

NSArray * sortedByDateArray = [ unsortedArray sortedArrayUsingComparator:^( id a, id b ){
    return [ [ a sortDate ] compare:[ b sortDate ] ] ;
}];

编辑 或者使用排序描述符:

NSArray * sortedArray = [ unsortedArray sortedArrayUsingDescriptors:[ NSArray arrayWithObject:[ NSSortDescriptor sortDescriptorWithKey:@"sortDate" ascending:YES ] ] ] ;

You could add a category to unify the date field name. Let's say class A's instances have a field "dateString" and class B's instances have a field "creationDateString"... You could do this:

@implementation A (SortByDate)

-(NSDate*)sortDate
{
    static NSDateFormatter * formatter = nil ;
    static dispatch_once_t once = 0 ;
    dispatch_once( & once, ^{ formatter = ... } ) ;

    return [ formatter dateFromString:self.dateString ] ;
}
@end

@implementation B (SortByDate)
-(NSDate*)sortDate
{
    static NSDateFormatter * formatter = nil ;
    static dispatch_once_t once = 0 ;
    dispatch_once( & once, ^{ formatter = ... } ) ;

    return [ formatter dateFromString:self.creationDateString ] ;
}
@end

Now you can sort all objects in your array by their sortDate property:

NSArray * sortedByDateArray = [ unsortedArray sortedArrayUsingComparator:^( id a, id b ){
    return [ [ a sortDate ] compare:[ b sortDate ] ] ;
}];

EDIT Or with a sort descriptor:

NSArray * sortedArray = [ unsortedArray sortedArrayUsingDescriptors:[ NSArray arrayWithObject:[ NSSortDescriptor sortDescriptorWithKey:@"sortDate" ascending:YES ] ] ] ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文