0

All,

In a mutable Array I have multiple Arrays, In each Array i have multiple dictionaries, In the Dictionaries i have common Values, Based on the Values I have to Group an Array/Dictionary below is sample Array

(

(
    {
        name = "attribute_Subject";
        value = English;
    },
    {
        name = "attribute_class";
        value = Fourth;
    },
),
(
    {
        name = "attribute_Subject";
        value = English;
    },
    {
        name = "attribute_class";
        value = Fifth;
    },
),
(
    {
        name = "attribute_Subject";
        value = Maths;
    },
    {
        name = "attribute_class";
        value = Fourth;
    },
),
(
    {
        name = "attribute_Subject";
        value = Science;
    },
    {
        name = "attribute_class";
        value = Fourth;
    },
),
(
    {
        name = "attribute_Subject";
        value = English;
    },
    {
        name = "attribute_class";
        value = Sixth;
    },
),
(
    {
        name = "attribute_Subject";
        value = Maths;
    },
    {
        name = "attribute_class";
        value = Sixth;
    },
),

)

If you see the Arrays We have English in Three Arrays, I want that three Arrays in a separate Array and same for "Science" and Maths

Can we use predicates?

Can any one help

1
  • Could you show expected result? Commented Aug 2, 2017 at 8:06

2 Answers 2

1

I am not entirely sure I understand what you are asking for, but I think this should help:

Objective-C:

//1
NSMutableArray* englishArray = [[NSMutableArray alloc]init];
NSMutableArray* scienceArray = [[NSMutableArray alloc]init];
NSMutableArray* mathArray = [[NSMutableArray alloc]init];

//2
for(int i = 0; i < mainArray.count; i++){
    //3
    if([mainArray[i][0][@"value"] isEqual: @"English"]){
        //4
        [englishArray addObject:mainArray[i]];
    }
    else if([mainArray[i][0][@"value"] isEqual: @"Science"]){
        [scienceArray addObject:mainArray[i]];
    }
    else if([mainArray[i][0][@"value"] isEqual: @"Math"]){
        [mathArray addObject:mainArray[i]];
    }
}

Explanation of above code:

  1. create 3 NSMutableArrays to store the three subject arrays.
  2. Iterate through all arrays within the mainArray (where mainArray is the full array you provided)
  3. To check subject, first access array in mainArray at index i.

eg. mainArray[i] :

(
    {
        name = "attribute_Subject";
        value = English;
    },
    {
        name = "attribute_class";
        value = Fourth;
    },
),

3.(cont.) Then access 1st element in that array we just accessed. (the first element is the dictionary which contains the subject value)

eg. mainArray[i][0] :

{
    name = "attribute_Subject";
    value = English;
},

3.(cont.2)Then access the value for key @"value" of that dictionary.

eg. mainArray[i][0][@"value"]:

@"English"

4.If that key is equal to the one we need, add the array to appropriate subject array.

Sign up to request clarification or add additional context in comments.

Comments

0

I think this code will be helpful. NSLog(@"%@", muteArray);

 NSArray *valuesArray = [muteArray valueForKey:@"value"];
    NSMutableArray *valuesArray1 = [[NSMutableArray alloc]init];
    for(NSArray *arr in valuesArray) {
        [valuesArray1 addObject:[arr objectAtIndex:0]];
    }
    NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:valuesArray1];
    NSArray *arrayWithoutDuplicates = [orderedSet array];

    NSArray *filtered = [muteArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"value Contains[d] %@", [arrayWithoutDuplicates objectAtIndex:0]]];

After getting arrayWithoutDuplicates using for loop you can get separate arrays.

4 Comments

Thanks Phani, But one thing i missed to mention, We can cannot directly compare with "English" or any other, the values are from server and we dont know any value.
You need to separate an array based on subject name?
No based on the Value only
Could you post your excepted result for above example.

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.