-1

filter the array remove duplicates of key value city i need to display only city names in first tableview without duplicates and when did select i need to show particular departments in next tableview

example for india i need to show three departments in next view Please suggest me

Array values :(
        {
        city = uk;
        department = "Sales support";

    },
        {

        city = us;
        department = "Sales support";

    },
        {
          city = italy;
          department = "Sales support";
           },
        {

        city = india;
        department = "x";
    },
        {

        city = india;
        department = "y";
            },
        {

        city = india;
        department = "z";
           },

   )
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
VishnuVardhan
  • 17
  • 1
  • 3
  • please check this [link][1] [1]: http://stackoverflow.com/questions/1025674/the-best-way-to-remove-duplicate-values-from-nsmutablearray-in-objective-c It will give you Distinct value. – chirag Apr 08 '13 at 07:17
  • you want only x, y, z passed to next VC? – Anoop Vaidya Apr 08 '13 at 07:17

4 Answers4

2
for (int i = 0; i< responseArray.count; i++)
{
    if (![cityArray containsObject:[[responseArray objectAtIndex:i] valueForKey:@"city"]])
    {
          [cityArray addObject:[[responseArray objectAtIndex:i] valueForKey:@"city"]];
    }
}

load the first tableView with the cityArray

in didSelectRowAtIndex:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"city = %@",[cityArray objectAtIndex:indexPath.row]];
NSArray *array = [responseArray filteredArrayUsingPredicate:predicate];
NSLog(@"Array -- %@",array);
Manu
  • 4,730
  • 2
  • 20
  • 45
0

Use the following code provide your output to rawJSONOutput

   NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray * sortedArray = [[NSMutableArray alloc] initWithArray:[rawJSONOutput sortedArrayUsingDescriptors:sortDescriptors]];
Vinodh
  • 5,262
  • 4
  • 38
  • 68
0

You can use NSPredicate to filter array/dictionary with queries.

Example:

NSArray *someArray()
{
    NSString *const city = @"city";
    NSString *const department = @"department";
    NSString *const uk = @"uk";
    NSString *const us = @"us";
    NSString *const italy = @"italy";
    NSString *const india = @"india";

    return @[
     @{ city: uk, department: @"Sales support" },
     @{ city: us, department: @"Sales support" },
     @{ city: italy, department: @"Sales support" },
     @{ city: india, department: @"x" },
     @{ city: india, department: @"y" },
     @{ city: india, department: @"z" },
    ];
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {

        NSArray *array = someArray();

        NSLog(@"%@", array);

        // You can make many fancy queries.
        NSPredicate *p = [NSPredicate predicateWithFormat:@"SELF.city like 'india'"];
        NSArray *b = [array filteredArrayUsingPredicate:p];

        NSLog(@"%@", b);

    }
    return 0;
}
HKTonyLee
  • 3,111
  • 23
  • 34
0

While making the dictionary of array itself, check whether the city has already exist,if it is., add the department using another key like department1,department 2..

Array values :(
        {
        city = uk;
        department = "Sales support";

    },
        {

        city = us;
        department = "Sales support";

    },
        {
          city = italy;
          department = "Sales support";
           },
        {

        city = india;
        department1 = "x";
        department2 = "y";
       department3 = "z";
    },

   )

better try to make like this. This will be be easy.

wesley
  • 859
  • 5
  • 15