0

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>
2
  • Can you post the structure of one of the messages? For example, what do you see if you NSLog(@"First message: %@", [message objectAtIndex:0]);? Commented Jun 9, 2014 at 12:24
  • @Stonz2 i've updated the question with the example. Commented Jun 9, 2014 at 12:31

1 Answer 1

1

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"];
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! It's brilliant, working fine (i changed only msg.keySenderUser to msg.message[@"keySenderUser"]), except one thing. Actually i have messages from two users called sampleUser1 and sampleUser2, but when i log it, it looks like this:` { sampleUser1 = ( content ); sampleUser2 = ( content ); sampleUser1 = ( content );}` Because of something sampleUser1 is duplicated. Do you have any idea what can cause this? It's good in the allUsers array, it contains only two usernames.
Are you sure the user names are identical? For instance, are there subtle differences when sampleUser1 is sender vs receiver?
Problem solved. I was wrong, because the log was inside the loop. It made two logs because there was two users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.