簡體   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