2010-08-16 64 views
0

我有称为站在我的系统具有这些属性的对象:Objective-C中由特定对象对象的排序的NSMutableArray属性

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

我也有含有如上所定义的20“站”对象一个NSMutableArray。

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

有人能请解释一下我该怎么办呢?

+0

的重复【如何一个NSMutableArray与它的自定义对象进行排序?](http://stackoverflow.com/questions/805547/how-to-sort- an-nsmutablearray -with-custom-objects-in-it) – 2010-08-16 05:32:32

回答

3

我会使用

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

我会推荐返回签名是NSComparisionResult – vodkhang 2010-08-16 05:58:09

+0

vodkhang,我同意,我的错误。 – kovpas 2010-08-16 07:18:14