1

I have an array with multiple locations for different states.

{"location":"Lekki Phase 1","state":"abc","country":"Nigeria"},
{"location":"Lekki Phase 2","state":"xyz","country":"Nigeria"},
{"location":"Osapa London1","state":"def","country":"Nigeria"},
{"location":"Lekki Phase 2","state":"abc","country":"Nigeria"},
{"location":"Lekki Phase 3","state":"xyz","country":"Nigeria"},
{"location":"Osapa London 2","state":"def","country":"Nigeria"},..........

Now i can make an array for different states with no duplicate state , like

 {"abc","xyz","def"}

But what i want is to display all locations state wise in a table.

How can i do this??

6
  • 3
    No. First you tell me why this is tagged xcode. Commented Mar 15, 2013 at 5:58
  • could you please paste few line of your array code ? Commented Mar 15, 2013 at 6:02
  • do you want to use multi section table or single section? Commented Mar 15, 2013 at 6:03
  • 1
    Simple, Sort your array by state and then use it. Commented Mar 15, 2013 at 6:03
  • this might helps you :) stackoverflow.com/questions/2766994/… Must Commented Mar 15, 2013 at 6:03

3 Answers 3

1

Using NSPredicate we can efficiently filter this . I have tried and tested working for me.

Here 'allDataArray' is array with dictionaries, You can replace your array here (the first one in your post)

    NSMutableArray *allDataArray = [[NSMutableArray alloc] init];
    NSMutableDictionary *dict1 = [[NSMutableDictionary alloc] init];
    [dict1 setObject:@"Lekki Phase 1" forKey:@"location"];
    [dict1 setObject:@"abc" forKey:@"state"];
    [dict1 setObject:@"Nigeria" forKey:@"country"];
    [allDataArray addObject:dict1];

    dict1 = [[NSMutableDictionary alloc] init];
    [dict1 setObject:@"Lekki Phase 2" forKey:@"location"];
    [dict1 setObject:@"xyz" forKey:@"state"];
    [dict1 setObject:@"Nigeria" forKey:@"country"];
    [allDataArray addObject:dict1];

    dict1 = [[NSMutableDictionary alloc] init];
    [dict1 setObject:@"Lekki Phase 2" forKey:@"location"];
    [dict1 setObject:@"abc" forKey:@"state"];
    [dict1 setObject:@"Nigeria" forKey:@"country"];
    [allDataArray addObject:dict1];

    dict1 = [[NSMutableDictionary alloc] init];
    [dict1 setObject:@"Lekki Phase 3" forKey:@"location"];
    [dict1 setObject:@"xyz" forKey:@"state"];
    [dict1 setObject:@"Nigeria" forKey:@"country"];
    [allDataArray addObject:dict1];
    //NSLog(@"%@",allDataArray);

    NSArray *state = [NSArray arrayWithObjects:@"abc",@"xyz", nil];

    NSMutableArray *locationInState = [[NSMutableArray alloc] initWithCapacity:[state count]];
    for(int i=0; i< [state count]; i++)
    {
        NSMutableArray *filteredarray = [[allDataArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(state == %@)", [state objectAtIndex:i]]] mutableCopy];
        for(int j=0; j<[filteredarray count];j++)
        {
            NSDictionary *dict = [filteredarray objectAtIndex:j];
            [filteredarray replaceObjectAtIndex:j withObject:[dict valueForKey:@"location"]];
        }
        [locationInState addObject:filteredarray];
    }
    NSLog(@"%@",locationInState);

Here locationInState array contains all location for filetred state. You can map them easily by index.

Result is

(
    (
        "Lekki Phase 1",
        "Lekki Phase 2"
    ),
        (
        "Lekki Phase 2",
        "Lekki Phase 3"
    )
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for reply. What does "all" refers to here??
allDataArray is array with dictionary the first one you have. just try and let me know if works for you.
I have tried your code but it crashed with error 'NSUnknownKeyException', reason: '[<__NSCFString 0x9ee8e50> valueForUndefinedKey:]: this class is not key value coding-compliant for the key state.
1

First It not array but it is Dictionary.

NSMutableDictionary * newDict = [NSMutableDictionary dictionaryWithCapacity:[OLdDict count]];
for(id item in [OLdDict allValues]){
    NSArray * keys = [OLdDict allKeysForObject:item];
    [newDict setObject:item forKey:[[OLdDict allKeysForObject:item] objectAtIndex:0]];
}

Comments

1

NSMutableDictionaries can act as uniquing collections (because they replace objects for keys if the same key is used twice). We can also take advantage of the fact that NSString is generally a constant address location, and funnel each one of those dictionaries into an array. To unique out each array of dictionaries, it would be far easier to wrap them in an object, but here goes:

-(void)uniquingSort {
    //Setup collections for the uniquing process
    NSMutableArray *datasource = //...
    NSMutableIndexSet *hits = [NSMutableIndexSet indexSet];
    NSMutableDictionary *uniquingDict = @{}.mutableCopy;

    //Setup an index for the indexed set
    int idx = 0;

    //iterate through the array of dictionaries
    for (NSArray *arrOfDicts in datasource) {
        //get the dictionary we want to unique against
        NSDictionary *innerDict = arrayOfDicts[1];
        //do we have a dupe?  If so, add its index to the index set
        if (uniquingDict[innerDict[@"state"]] != nil)
            [hits addIndex:idx];
        uniquingDict[innerDict[@"state"]] = innerDict[@"state"];
        idx++;
    }
    //cut out all the hits till we are only uniqued for the "state" key
    [datasource removeObjectsAtIndexes:hits];
}

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.