How to establish secondary NSSortDescriptor sort key?

自作多情 提交于 2019-12-05 16:33:40

问题


I have successfully sorted the data I have by my sort key lastName, but I want to know how to sort by lastName, and then by firstName. Here is the code I used to sort by lastName

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

How do I add the secondary sort key of firstName?


回答1:


NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];

[request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];



回答2:


Notice that you are passing an array of sort descriptors. Simply create another descriptor for firstname and create the array with both descriptors. They will be applied in the order of the array.



来源:https://stackoverflow.com/questions/8410637/how-to-establish-secondary-nssortdescriptor-sort-key

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