iOS根据另一个数组中的字符串顺序对一个数组进行排序 [英] iOS sort one array based on order of strings in another array

查看:783
本文介绍了iOS根据另一个数组中的字符串顺序对一个数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要解决的另一个非常具体的问题.

This is another very specific problem I am trying to solve.

我正在提取一个列表,其中列出了登录到用户设置应用程序的Twitter用户帐户.这将以正确的顺序返回包含用户名的数组.

I am pulling a list a twitter user accounts logged into the users settings application. This returns an array with the usernames in the correct order.

然后我将此数组传递给此twitter API:

I then pass this array to this twitter API:

https://api.twitter.com/1.1/users/lookup.json

返回的数组包含我登录的每个用户帐户所需的所有其他数据.第一个数组包含NSString(用户名),返回的数组已解析为包含字典,字典中包含用户名,名称,和个人资料照片.

The returned array contain all the additional data I need for each user account logged in. The first array contains NSStrings (usernames), the returned array has been parsed to contain dictionaries that have a key and value for the username, name, and profile pic.

现在的问题是顺序与我传递的第一个数组完全不同..这是Twitter的预期行为,但需要完全相同的顺序(我将引用AccountStore的原始索引,该顺序将匹配第一个数组,但不匹配新的字典数组.

Problem now is that the order is completely different than the first array I passed.. This is expected behavior from Twitter, but it needs to be in the exact same order (I will be referencing the original index of the AccountStore which will match the first array, but not the new array of dictionaries).

如何基于用户名键告诉新数组匹配包含的字典,使其与第一个数组的顺序相同?

How can I tell the new array to match the contained dictionaries to be the same order as the first array based on the username key?

我知道这听起来很混乱,所以至少让我发布数据来提供帮助.

I know this sounds confusing, so let me at least post the data to help.

这是第一个数组输出:

(
    kbegeman,
    indeedyes,
    soiownabusiness,
    iphonedev4me
)

这是第二个数组的输出:

Here is what the second array outputs:

(
        {
        image = "https://si0.twimg.com/profile_images/3518542448/3d2862eee546894a6b0600713a8de862_normal.jpeg";
        name = "Kyle Begeman";
        "screen_name" = kbegeman;
    },
        {
        image = "https://si0.twimg.com/profile_images/481537542/image_normal.jpg";
        name = "Jane Doe";
        "screen_name" = iPhoneDev4Me;
    },
        {
        image = "https://twimg0-a.akamaihd.net/profile_images/378800000139973355/787498ff5a80a5f45e234b79005f56b5_normal.jpeg";
        name = "John Doe";
        "screen_name" = indeedyes;
    },
        {
        image = "https://si0.twimg.com/sticky/default_profile_images/default_profile_5_normal.png";
        name = "Brad Pitt";
        "screen_name" = soiownabusiness;
    }
)

由于Twitter返回数据的方式,它从来都不是完全相同的顺序,因此每次调用这些方法时都必须进行检查.

Due to the way Twitter returns the data, it is never the EXACT same order, so I have to check this every time I call these methods.

任何帮助都会很大,可以挽救我的一夜.预先感谢!

Any help would be great, would save my night. Thanks in advance!

推荐答案

您希望通过将screen_name值与第一个数组进行比较来对字典数组进行排序.正确的?此外,屏幕名称的大小写可能与您的用户名不同.对吧?

You want the array of dictionaries be sorted by comparing screen_name value with your first array. Right? Also, the screen name may have different case than your username. Right?

我会使用映射字典:

  1. 屏幕名称用户词典创建字典:

NSArray *screenNames = [arrayOfUserDicts valueForKeyPath:@"screen_name.lowercaseString"];
NSDictionary *userDictsByScreenName = [NSDictionary dictionaryWithObjects:arrayOfUserDicts forKeys:screenNames];

  • 通过查找数组中用户名的用户字典来构建最终数组:

  • Build final array by finding user dictionary for usernames in your array:

    NSMutableArray *sortedUserDicts = [NSMutableArray arrayWithCapacity:arrayOfUsernames.count];
    for (NSString *username in arrayOfUsernames) {
        NSDictionary *userDict = [userDictsByScreenName objectForKey:username.lowercaseString];
        [sortedUserDicts addObject:userDict];
    }
    

  • 这篇关于iOS根据另一个数组中的字符串顺序对一个数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆