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.
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);
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]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With