Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort array of CoreData objects by integer attributes?

I have an array of CoreData objects, each object is Person and each Person has an age attribute which is of type Integer32.

My array is filled with Person objects. I want to sort my array by their age attribute.

How can I do this?

like image 692
Josh Kahane Avatar asked Feb 19 '23 10:02

Josh Kahane


1 Answers

It should be as simple as:

NSArray *sortDescriptors = @[
  [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]
];

NSArray *sortedPeople = [people sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"%@", sortedPeople);

This will work regardless of whether you do or do not choose to "Use scalar properties for primitive data types" when creating your NSManagedObject subclass (if you decide to even create them)

like image 70
Paul.s Avatar answered Mar 08 '23 13:03

Paul.s