![](/img/trans.png)
[英]Objective-C: Sort NSMutableArray of NSDates by weekday
[英]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];
看看NSPredicates。 可以用于查询和排序数组中的对象。
和NSSortDescriptor-带有示例。 http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.