简体   繁体   English

用对象订购NSArray

[英]Order NSArray with objects

I have an NSDictionary with the following data: 我有以下数据的NSDictionary

(lldb) po allFriends
{
    71685207018702188 =     {
        id = 71685207018702188;
        name = "mikeziri ";
        username = mi;
    };
    93374822540641772 =     {
        id = 93374822540641772;
        name = "Alan Weclipse";
        username = zuka;
    };
    96553685978449395 =     {
        id = 96553685978449395;
        name = "Monica Weclipse";
        username = amonica;
    };
    96556113096345076 =     {
        id = 96556113096345076;
        name = Xavier;
        username = branko;
    };
    97017008427632119 =     {
        id = 97017008427632119;
        name = "Dario Weclipse";
        username = tarzan;
    };
}

I'm sorting these objects based on the name , if they don't have a name , i will use the username . 我根据名称对这些对象进行排序,如果它们没有名称 ,我将使用用户名 To do that, i create a new NSDictionary with the name and id and at the end of the method i sort them by name . 为此,我用名称id创建了一个新的NSDictionary ,并在方法结尾按名称对它们进行排序。 The code to sort them is the following: 对它们进行排序的代码如下:

- (NSArray*)orderFriends
{
    NSMutableDictionary* newFriendsDict = [[NSMutableDictionary alloc] init];

    for (int i=0; i<[allFriends count];i++)
    {
        NSMutableDictionary* friendsDict = [[NSMutableDictionary alloc] init];
        NSDictionary* friend = [allFriends objectForKey:[NSString stringWithFormat:@"%@", [sortedKeysFriends objectAtIndex:i]]];

        if ([[friend objectForKey:@"name"] length] != 0)
        {
            [friendsDict setObject:[friend objectForKey:@"id"] forKey:@"id"];
            [friendsDict setObject:[NSString stringWithFormat:@"%@", [friend objectForKey:@"name"]] forKey:@"name"];
        }
        else
        {
            [friendsDict setObject:[friend objectForKey:@"id"] forKey:@"id"];
            [friendsDict setObject:[NSString stringWithFormat:@"%@", [friend objectForKey:@"username"]] forKey:@"name"];
        }

        [newFriendsDict setObject:friendsDict forKey:[NSNumber numberWithInt:i]];
    }

    NSArray* sp = nil;
    sp = [[newFriendsDict allValues] sortedArrayUsingComparator:^(id obj1, id obj2){
        NSString *one = [NSString stringWithFormat:@"%@", [obj1 objectForKey:@"name"]];
        NSString *two = [NSString stringWithFormat:@"%@", [obj2 objectForKey:@"name"]];

        return [one compare:two];
    }];

    return sp;
}

The problem is that the end result is wrong: 问题是最终结果是错误的:

(lldb) po sp
<__NSArrayI 0x160491a0>(
{
    id = 93374822540641772;
    name = "Alan Weclipse";
},
{
    id = 97017008427632119;
    name = "Dario Weclipse";
},
{
    id = 96553685978449395;
    name = "Monica Weclipse";
},
{
    id = 96556113096345076;
    name = Xavier;
},
{
    id = 71685207018702188;
    name = "mikeziri ";
},
)

Case sensitive. 区分大小写。 make all string small or big. 使所有字符串变小或变大。

You could also just change 你也可以改变
return [one compare:two];
to
return [one compare:two options: options:NSCaseInsensitiveSearch];

Than it will be ordered alphabetically, no matter if upper or lower case... 无论字母是大写还是小写,它都将按字母顺序排序...

Several things: There is no reason to build different dictionaries in order to sort, and good reason NOT to do so. 有几件事情:没有理由为了排序而建立不同的字典,没有理由这样做。

You already found the method sortedArrayUsingComparator. 您已经找到了sortedArrayUsingComparator方法。 That takes a block that is used to compare pairs of objects, and returns a sorted array. 这需要一个用于比较对象对的块,并返回一个排序后的数组。 You can use that method to implement any sorting criteria you want. 您可以使用该方法来实现所需的任何排序条件。

I would suggest writing a comparator block that compares the name properties of your objects unless it's blank, and uses username if that's blank. 我建议编写一个比较器块,比较对象的名称属性(除非为空),如果该用户名为空,则使用用户名。 It would only be a few lines of code: 只需几行代码:

NSArray *sortedFriends = [[allFriends allValues] sortedArrayUsingComparator:
                          ^(NSDictionary *obj1, NSDictionary *obj2)
{
    NSString* key1 = obj1[@"name"] ? obj1[@"name"] : obj1[@"username"];
    NSString* key2 = obj2[@"name"] ? obj2[@"name"] : obj2[@"username"];
    return [key1 caseInsensitiveCompare: key2];
}];

EDIT: I just noticed (from your edit of my post) that you are starting from a dictionary, not an array. 编辑:我刚刚从您对帖子的编辑中注意到,您是从字典而不是数组开始的。 So what you want to do is to create a sorted array of all the values in the dictionary? 因此,您要做的是创建字典中所有值的排序数组? Is it acceptable to discard the keys for all the items in your dictionary, and end up with a sorted array of the values? 舍弃字典中所有项目的键,并以值的排序数组结束是否可以接受?

The other thing you could do would be to build an array of the dictionary keys, sorted based on your sort criteria. 您可以做的另一件事是建立一个字典键数组,并根据您的排序标准对其进行排序。 Then you could use the array of keys to fetch the items from your dictionary in sorted order. 然后,您可以使用键数组按排序顺序从字典中获取项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM