简体   繁体   中英

Sorted NSMutableArray from NSArray

I have an NSArray called message , it contains an unsorted list of custom objects. Every object has the same structure, it has an NSDictionary and two other string in it.

Every object is a message and i would like to sort them based on the dictionary key keySenderUser and keyReceiverUser . 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". 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. 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. If Bill is the sender, current user is the receiver, if Bill is the receiver, current user is the sender. 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. 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

[sortedMessages objectForKey:@"Bill"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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