繁体   English   中英

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

[英]Objective-C Sort NSMutableArray of Objects by a specific Object attribute

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

@interface Station : NSObject {
NSString *stationID;
NSString *callsign;
NSString *stationState;
}

我也有一个NSMutableArray,其中包含20个以上定义的“ Station”对象。

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

有人可以解释一下我该怎么做吗?

我会用

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];

暂无
暂无

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

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