NSSortdescriptor, finding the path to the key I wish to use for sorting

隐身守侯 提交于 2019-12-22 17:49:15

问题


I am using an NSSortdescriptor to sort a collection of NSArrays, I then came across a case where the particular NSArray to be sorted contains an NSDictionary who contains an NSDictionary. I would like to sort from the string paired with a key in the last dictionary.

This is how I would reference the string:

NSDictionary *productDict = [MyArray objectAtIndex:index];
NSString *dealerName = [[productDict objectForKey:@"dealer"] objectForKey:@"name"];

How would I use the dealerName in my NSSortdescriptor to sort the array?

NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:/* ? */  ascending:YES];
sortedDealerArray = [value sortedArrayUsingDescriptors:sortDesc];
[sortDesc release];

Hope someone could help me a bit with how I go about sorting according to keys inside objects inside other objects:)

Thank you.


回答1:


actually warrenM is right. You can just use keypaths. @"dealer.name" should work for above.



来源:https://stackoverflow.com/questions/2866418/nssortdescriptor-finding-the-path-to-the-key-i-wish-to-use-for-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!