Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

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.

Amit Bijlani, Ben Jakuben do you have any idea how can i do this?

2 Answers

Try this: assuming messages is your unsorted array and your custom class is called Message containing a dictionary called messageDetails which holds your keySenderUser and keyReceiverUser

For simplification I assume your user is just a string. I guess it's an object though, so you might need to use a comparison operator (==) instead of isEqualToString

NSString *currentUser = @"User";
NSMutableDictionary *messagesByUser = [NSMutableDictionary dictionary];

for (Message *message in messages) {
        NSDictionary *messageDetails = message.messageDetails;
        NSString *sender = messageDetails[@"keySenderUser"];
        NSString *receiver = messageDetails[@"keyReceiverUser"];
        if ([currentUser isEqualToString:sender]){
            NSMutableArray *userMessages = [NSMutableArray array];
            if ([messagesByUser objectForKey:receiver]) {
                userMessages = [messagesByUser objectForKey:receiver];

            }
            [userMessages addObject:message];
            [messagesByUser setObject:userMessages forKey:receiver];
        }
        else if ([currentUser isEqualToString:receiver]){
            NSMutableArray *userMessages = [NSMutableArray array];
            if ([messagesByUser objectForKey:sender]) {
                userMessages = [messagesByUser objectForKey:sender];

            }
            [userMessages addObject:message];
            [messagesByUser setObject:userMessages forKey:sender];
        }
 }