![](/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.