iOS 如何对包含NSNumber 的NSarray 的自定义对象的NSMutableArray 进行排序?
iOS how to sort NSMutableArray of custom object contained NSarray of NSNumber?
我有一个包含 StudentScoreObj 的数组 (NSMutableArray)
,对象有 missions
(NSArray)
.
喜欢:
missions = @[@34, @43, @54, @23, @54];
need to sort StudentScoreObj of array using first object of missions array.
我的代码:
@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
您可以使用 sortedArrayUsingComparator
。
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];
}];
我有一个包含 StudentScoreObj 的数组 (NSMutableArray)
,对象有 missions
(NSArray)
.
喜欢:
missions = @[@34, @43, @54, @23, @54];
need to sort StudentScoreObj of array using first object of missions array.
我的代码:
@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
您可以使用 sortedArrayUsingComparator
。
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];
}];