Iphone Objective-C按特定对象属性对NSMutableArray对象进行排序

Iphone Objective-C按特定对象属性对NSMutableArray对象进行排序,iphone,objective-c,sorting,nsmutablearray,Iphone,Objective C,Sorting,Nsmutablearray,我的系统中有一个名为Station的对象,该对象具有以下属性: @interface Station : NSObject { NSString *stationID; NSString *callsign; NSString *stationState; } 我还有一个NSMutableArray,包含上面定义的20个“Station”对象 我需要定义一个方法,该方法可以通过两种方式对该数组进行排序: 1) 按stationID 2) 呼号 有人能解释一下我是如何做到这一点的吗?看看NSPr

我的系统中有一个名为Station的对象,该对象具有以下属性:

@interface Station : NSObject {
NSString *stationID;
NSString *callsign;
NSString *stationState;
}
我还有一个NSMutableArray,包含上面定义的20个“Station”对象

我需要定义一个方法,该方法可以通过两种方式对该数组进行排序: 1) 按stationID 2) 呼号


有人能解释一下我是如何做到这一点的吗?

看看NSPredicates。这可用于查询和排序数组中的对象

这里也有例子。

和NSSortDescriptor-附示例。

看看NSPredicates。这可用于查询和排序数组中的对象

这里也有例子。

和NSSortDescriptor-附示例。 我会用

NSInteger stationsSort( Station *station1, Station *station2, void *context) {
    if ( station1_greater_than_station2 )   {
        return NSOrderedDescending;
    }

    if ( station1_less_than_station2 ) {
        return NSOrderedAscending;
    }

    return NSOrderedSame;   
}

[myArray sortedArrayUsingFunction:stationsSort context:nil];
我会用

NSInteger stationsSort( Station *station1, Station *station2, void *context) {
    if ( station1_greater_than_station2 )   {
        return NSOrderedDescending;
    }

    if ( station1_less_than_station2 ) {
        return NSOrderedAscending;
    }

    return NSOrderedSame;   
}

[myArray sortedArrayUsingFunction:stationsSort context:nil];

我建议返回签名的副本是nscomparisonresultvodkhang,我同意,我的错误。我建议返回签名是nscomparisonresultvodkhang,我同意,我的错误。