Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping of Custom Objects in Objective-C

I have and array of custom objects of Person Class

Person : NSObject{
    NSString *firstName;
    NSString *lastName; 
    NSString *age;
}

NSMutableArray *personsArray = [NSMutableArray array];
Person  *personObj1 =  [[[Person alloc]  init] autorelease];
personObj1.firstName = @"John";
personObj1.lastName = @"Smith";
personObj1.age = @"25";
[personsArray addObject: personObj1];

Person  *personObj2 =  [[[Person alloc]  init] autorelease];
personObj2.firstName = @"John";
personObj2.lastName = @"Paul";
personObj2.age = @"26";
[personsArray addObject: personObj2];

Person  *personObj3 =  [[[Person alloc]  init] autorelease];
personObj3.firstName = @"David";
personObj3.lastName = @"Antony";
personObj3.age = @"30";
[personsArray addObject: personObj3];

Now personsArray contains 3 objects of Person objects.

Can we group this objects by attribute like age or firstName ?

My expected result is

NSDictionary {
    "John" = >{
        personObj1, //(Its  because personObj1 firstName is John )
        personObj2 //(Its  because personObj2 firstName is John )
    },
    "David" = >{
        personObj3, //(Its  because personObj3 firstName is David )
    },
}

I know I can get this result by creating an NSDictionary and then Iterate through personsArray, then check for each first

NSMutableDictionary *myDict = [NSMutableDictionary dictionary];

  for (Person *person in personsArray){

    if([myDict objectForKey: person.firstName]){
        NSMutableArray *array = [myDict objectForKey: person.firstName];
        [array addObject:person];
        [myDict setObject: array forKey:person.firstName];
    }else{
        NSMutableArray *array = [NSMutableArray arrayWithObject:person];
        [myDict setObject: array forKey:person.firstName];
    }
    }

    NSLog(@"myDict %@", myDict);

//Result myDict will give the desired output. 

But is there any better way to do ?

If I am using @distinctUnionOfObjects , I can group by string objects only (Not such custom objects. Am I right ?).

Thanks for the answer in advance.

like image 562
S.P. Avatar asked May 30 '12 12:05

S.P.


2 Answers

Checkout this answers it is very similar to what you want

https://stackoverflow.com/a/15375756/1190861

The code in your case will be as the following:

NSMutableDictionary *result = [NSMutableDictionary new];

NSArray *distinctNames;
distinctNames = [personsArray valueForKeyPath:@"@distinctUnionOfObjects.firstName"];
for (NSString *name in distinctNames) {
    NSPredicate predicate = [NSPredicate predicateWithFormat:@"firstName = %@", name]; 
    NSArray *persons = [personsArray filteredArrayUsingPredicate:predicate];
    [result setObject:persons forKey:name];
}

NSLog(@"%@", result);
like image 67
Mahmoud Adam Avatar answered Oct 14 '22 17:10

Mahmoud Adam


You can try this is custom object sorting for an array

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Firstname" ascending:NO selector:@selector(compare:)];

[Array sortUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
like image 3
maheswaran Avatar answered Oct 14 '22 15:10

maheswaran