kailoon
kailoon

Reputation: 2069

Is it possible to create an NSSortDescriptor for a dictionary value in an object?

I have an object with a dictionary property. I want to sort by a value in the dictionary, lets say,

myObject.dictionary[@"sort"]

I have an array of these objects. Is it possible to create an NSSortDescriptor to sort by that value? Like,

[NSSortDescriptor sortDescriptorWithKey:@"dictionary['sort']" ascending:YES]

Upvotes: 0

Views: 275

Answers (1)

utogaria
utogaria

Reputation: 3842

Yes, it's possible and very normal to sort an array of objects like that. You can simply create the sort descriptor in the following way:

NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"dictionary.sort" ascending:YES];

"dictionary.sort" will be used as keyPath for sorting your object.

Upvotes: 3

Related Questions