2017-03-16 59 views
1

我有一個數組(NSMutableArray)其中包含StudentScoreObj和對象有missions(NSArray)iOS如何對包含NSNumber的NSarray的自定義對象的NSMutableArray進行排序?

喜歡:

missions = @[@34, @43, @54, @23, @54]; 

需要使用任務陣列的第一對象進行排序陣列的StudentScoreObj。

我的代碼:

@interface StudentScoreObj : NSObject 

@property (nonatomic, copy) NSString *teamType; 
@property (nonatomic, copy) NSString *teamNumber; 
@property (nonatomic, copy) NSString *studentName; 
@property (nonatomic, copy) NSString *isParticipated ; 
@property (nonatomic, copy) NSString *totalScore; 
@property (nonatomic, copy) NSString *avgScore; 
@property (nonatomic, copy) NSArray *missions; 

@end 
+0

你是怎麼試過的? – Paulw11

回答

0

您可以使用sorted​Array​Using​Comparator了點。

NSArray *sortArray = [array sortedArrayUsingComparator:^NSComparisonResult(StudentScoreObj *obj1,StudentScoreObj *obj2) { 

    NSNumber *obj1Score = @0, *obj2Score = @0;   
    if([[obj1 missions] firstObject]) { 
     obj1Score = @([[[obj1 missions] firstObject] intValue]); 
    } 

    if([[obj2 missions] firstObject]) { 
     obj2Score = @([[[obj2 missions] firstObject] intValue]); 
    } 
    return [obj1Score compare:obj2Score]; 
}]; 
+0

它的作品。謝謝! – PillowPig

+0

@PillowPig歡迎伴侶:) –

相關問題