美文网首页
按照数组中的字符串顺序给另一个数组排序

按照数组中的字符串顺序给另一个数组排序

作者: 努努Nunu | 来源:发表于2021-06-07 13:35 被阅读0次

    数组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就是排序后的数组

    相关文章

      网友评论

          本文标题:按照数组中的字符串顺序给另一个数组排序

          本文链接:https://www.haomeiwen.com/subject/idrdlltx.html