0

I really tried not to ask the question here but after hours of trying, I'm stuck. So here goes... hopefully I can be clear.

My desired output is this:

({
    following = "";
    followingMe = "";
},{
    following = "";
    followingMe = "";
},{...})

The data coming in looks like this and what I want to do is sort it

({
    user = "me";
    following = "sam";
},{
    user = "sam";
    following = "me";
},{
    user = "foo";
    following = "me";
},{
    user = "me";
    following = "bar";
},...)

I want to organize it so if I'm following "sam" and he's following me, it's one dictionary. If "foo" is following me, then following="(null)" and followingMe="sam"...and so on. That's why I didn't want to ask the question... it's hard to explain.

Here's the code I have but it just gives me the last entry over and over...what's the typo?

NSMutableDictionary *temp = [NSMutableDictionary new];
int i = 0;
while (i < array.count) {
    [temp removeAllObjects];
    if ([[[array objectAtIndex:i]valueForKey:@"user"]isEqualToString:_user]){
    //means I'm following this person
        [temp setValue:[[array objectAtIndex:i] valueForKey:@"following"] forKey:@"following"];

        if ([[_allfriends valueForKey:@"followingMe"]containsObject:[[array objectAtIndex:i] valueForKey:@"following"]]) {
        //check to see if this person is already following me

            [[_allfriends objectAtIndex:[[_allfriends valueForKey:@"followingMe"] indexOfObject:
            [[array objectAtIndex:i] valueForKey:@"following"]]] setObject:[[array objectAtIndex:i] valueForKey:@"following"] forKey:@"following"];
            //trying to update at the index where this person is following me

        }else{
            [temp setObject:@"(null)" forKey:@"followingMe"];
            [_allfriends addObject:temp];
        }
    }else{
        //this person is following me
        [temp setObject:[[array objectAtIndex:i]valueForKey:@"user"] forKey:@"followingMe"];

        if ([[_allfriends valueForKey:@"following"]containsObject:[[array objectAtIndex:i] valueForKey:@"user"]]) {
        //check to see if I'm following this pseron

            [[_allfriends objectAtIndex:[[_allfriends valueForKey:@"following"] indexOfObject:
            [[array objectAtIndex:i] valueForKey:@"user"]]] setObject:[[array objectAtIndex:i] valueForKey:@"user"] forKey:@"followingMe"];
            //try to update followingMe at that index

        }else{
            [temp setObject:@"(null)" forKey:@"following"];
            [_allfriends addObject:temp];
        }
    }
    i++;
}

So the logic is I'm checking where my name is (user or following). Then I'm checking if the person is already followingMe. It it's an entry in the _allfriends, I need to get that index and I want to update the "following"...makes sense? What am I doing wrong here?

2 Answers 2

0

U can sort array with Set

SortedArr = [NSMutableArray arrayWithArray:[[NSSet setWithArray: UnsortedArr] allObjects]];
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure how that should help me but it just gave me the original array right back
sorry, but this will remove duplicate element from an array...not sorting
0

So apparently the problem was with the NSMutableDictionary *temp = [NSMutableDictionary new];. I'm not sure if this post applies but it made me rewrite my code. temp values were attached to the _allfriends array so when temp changed, EVERYTHING in _allfriends changed. My loop logic was correct but I rewrote it like this:

NSString *key = [NSString new];
for (int i = 0; i < array.count; i++) {
    if ([[[array objectAtIndex:i]valueForKey:@"user"]isEqualToString:_user]){
        key = [[array objectAtIndex:i] valueForKey:@"following"];
        if ([[_allfriends valueForKey:@"followingMe"]containsObject:key]) {
            [_allfriends removeObjectAtIndex:[[_allfriends valueForKey:@"followingMe"] indexOfObject:key]];
            [_allfriends insertObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"following", key, @"followingMe", nil] atIndex:_allfriends.count];
        }else{
            [_allfriends insertObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"following", @"(null)", @"followingMe", nil] atIndex:_allfriends.count];
        }
    }else{
        key = [[array objectAtIndex:i] valueForKey:@"user"];
        if ([[_allfriends valueForKey:@"following"]containsObject:key]) {
            [_allfriends removeObjectAtIndex:[[_allfriends valueForKey:@"following"] indexOfObject:key]];
            [_allfriends insertObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"following", key, @"followingMe", nil] atIndex:_allfriends.count];
        }else{
            [_allfriends insertObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"followingMe", @"(null)", @"following", nil] atIndex:_allfriends.count];
        }
    }
}

Hope that helps someone...

Comments

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.