数组1
NSArray *parAry = @[@{@"name":@"历史",@"value":@"90", @"grade":@"2"},
@{@"name":@"化学",@"value":@"92",@"grade":@"2"},
@{@"name":@"英语", @"value":@"98",@"grade":@"1"},
@{@"name":@"语文", @"value":@"96", @"grade":@"1"},
@{@"name":@"物理", @"value":@"91", @"grade":@"2"},
@{@"name":@"地理", @"value":@"90", @"grade":@"2"},
@{@"name":@"生物", @"value":@"98", @"grade":@"1"},
@{@"name":@"数学", @"value":@"96", @"grade":@"1"}];
数组2:
NSArray *keyAry = @[@"语文",@"数学", @"英语",@"物理",@"化学",@"生物",@"地理",@"历史"];
数组1按照数组2的顺序排序
//按KeyAry数组顺序给parAry排序
NSArray *screenNames = [parAry valueForKeyPath:@"name.lowercaseString"];
NSDictionary *userDictsByScreenName = [NSDictionary dictionaryWithObjects:parAry forKeys:screenNames];
NSMutableArray *sortedUserDicts = [NSMutableArray arrayWithCapacity:keyAry.count];
for (NSString *username in keyAry) {
NSDictionary *userDict = [userDictsByScreenName objectForKey:username.lowercaseString];
[sortedUserDicts addObject:userDict];
}
sortedUserDicts就是排序后的数组
网友评论