简体   繁体   English

从NSArray排序NSMutableArray

[英]Sorted NSMutableArray from NSArray

I have an NSArray called message , it contains an unsorted list of custom objects. 我有一个称为messageNSArray ,它包含自定义对象的未排序列表。 Every object has the same structure, it has an NSDictionary and two other string in it. 每个对象都具有相同的结构,其中包含一个NSDictionary和另外两个字符串。

Every object is a message and i would like to sort them based on the dictionary key keySenderUser and keyReceiverUser . 每个对象都是一条消息,我想根据字典键keySenderUserkeyReceiverUser对它们进行排序。 The new array should contain arrays, each array in the new array should represent the messages from a different user except the current user. 新数组应包含数组,新数组中的每个数组应代表来自当前用户以外的其他用户的消息。

For example i have 50 message in the base message array, it contains messages from Bill, John, Anna, Taylor and the "current user". 例如,我在基本message数组中有50条消息,其中包含来自Bill,John,Anna,Taylor和“当前用户”的消息。 I would like to put every message into a new array where John is the keySenderUser or keyRecieverUser and put to a new mutable array where i collect the messages of the users and do the same with Bill, John, Anna and Taylor. 我想将每个消息放入一个新数组,其中John是keySenderUserkeyRecieverUser并放入一个新的可变数组,在该数组中,我收集用户的消息,并对Bill,John,Anna和Taylor做同样的事情。 The result should be an array that looks like this: 结果应为如下所示的数组:

NSMutableArray *messagesFromUsers = @[messagesOfBill, messagesOfJohn, messagesOfAnna, messagesOfTaylor];

For example the messagesOfBill must contains the messages where Bill is the sender or the receiver that we know from the dictionary values. 例如, messagesOfBill必须包含消息,其中Bill是我们从字典值中知道的发送者或接收者。 If Bill is the sender, current user is the receiver, if Bill is the receiver, current user is the sender. 如果Bill是发送者,则当前用户是接收者,如果Bill是接收者,则当前用户是发送者。 The other part always is the current user. 另一部分始终是当前用户。 There is no messages where Anna is the sender and Bill is the receiver. 没有消息,其中安娜是发送者,比尔是接收者。

As a first step i think i need a list with the usernames and then iterate through the all messages and create a new array for every user and collect these arrays into a new array, but honestly i don't have any idea how should i do it. 第一步,我认为我需要一个包含用户名的列表,然后遍历所有消息并为每个用户创建一个新数组,并将这些数组收集到一个新数组中,但是老实说,我不知道该怎么办它。 I can remove objects from the message array based on one dictionary key, but that's all. 我可以基于一个字典键从消息数组中删除对象,仅此而已。 I'm not experienced to figure it out alone. 我没有经验独自解决。

UPDATE: This is how one object looks like in the message array: 更新:这是一个对象在消息数组中的样子:

NSLog(@"First message: %@", [message objectAtIndex:0]);
// result
    First message: PNMessage (0x175d49f0): <message: {
        keyCreateDate = \"06/08/14 21:23\";             
        keyMessage = "Lorem Ipsum ";
        keyRecieverChannel = vidra;
        keySenderUser = currentUsersName;
    }, date: (null), channel: currentUsersName>

You're definitely on the right track with your idea at the end of your post. 在文章末尾,您的想法绝对正确。 First, get the list of all users. 首先,获取所有用户的列表。

NSMutableArray *allUsers = [[NSMutableArray alloc] init];
for(PNMessage *msg in message)
{
    if(![allUsers containsObject:msg.keySenderUser])
        [allUsers addObject:msg.keySenderUser];
    else if(![allUsers containsObject:msg.keyReceiverUser])
        [allUsers addObject:msg.keyReceiverUser];
}
[allUsers removeObject:currentUsersName]; // don't need current user in the array

Now you have a list of all the usernames. 现在,您有了所有用户名的列表。 Loop through your message array for each user and divvy the messages. 循环遍历每个用户的message数组并划分消息。 I would recommend a dictionary entry for every user with the key being their name and the object being a mutable array of their messages. 我会为每个用户推荐一个字典条目,其键为用户名,对象为消息的可变数组。

NSMutableDictionary *sortedMessages = [[NSMutableDictionary alloc] init];
for(NSString *user in allUsers)
{
    NSMutableArray *messagesForUser = [[NSMutableArray alloc] init];
    for(PNMessage *msg in message)
    {
        if([msg.keySenderUser isEqualToString:user] || 
            [msg.keyReceiverUser isEqualToString:user])
        {
            [messagesForUser addObject:msg];
        }
    }
    [sortedMessages setObject:messagesForUser forKey:user];
    // if your messages list is exceptionally large, you could delete the messages you've already sorted here 
    // so that you don't have to look at messages you've already sorted
}

You now have an array with messages sorted by user. 您现在有了一个数组,其中包含按用户排序的消息。 You could bring up the array of messages from Bill by doing 您可以通过执行以下操作来显示来自Bill的消息

[sortedMessages objectForKey:@"Bill"];

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

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