多个排序描述符
当指定多个排序描述符时:
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:lastName, firstName, age, nil];
假设lastName、firstName和age都是NSSortDescriptor类型,并且具有由它们的名称建议的键。
我只是想了解当我这样做时会发生什么。假设我有一些核心数据(例如人员列表),并且我使用这些排序描述符对其进行排序。是否会尝试先对姓氏进行排序,然后当且仅当姓氏相同时,尝试按名排序(仅那些姓氏相同的记录),然后当且仅当名字和姓氏相同时,作为最后的手段,它会尝试按年龄排序(仅针对这些记录)。或者它会按姓氏顺序对列表进行排序,然后返回并按名字再次排序,然后再次返回并按年龄排序?
When specifying multiple sort descriptors:
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:lastName, firstName, age, nil];
Say lastName, firstName and age are all of type NSSortDescriptor and have keys suggested by their names.
I just want to understand what will happen when I do this. Say I have some core data (list of people, for example) and I sort it using these sort descriptors. Will it try to sort the last names first, then iff the last names are the same, try to sort (just those records where the last name is the same) by the first names, then iff the first names and last names are the same, it will try to sort (just for those records) by age as a last resort. Or will it sort the list in order of last name, then go back and sort it again by first name, then go back again and sort it by age?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它会做你想要的事情——第一个。我怀疑技术实现是否如您所描述的那样,但结果是相同的。
实现技术实现的一种方法是对每个进行直接排序,但以相反的顺序运行它们(三级排序,然后是二级排序,然后是一级排序)。这将产生您想要的结果的净效果,并且不需要每种排序都注意其他排序,而是在主键上生成排序,按辅助键子排序,子排序再次按第三键。但是,数据库能够使用的排序可能有更有效的实现。好消息是您不需要知道它是如何工作的——它就是知道。 :)
It does what you would want—the first one. I doubt the technical implementation is as you describe it but the result is the same.
One way the technical implementation could be achieved is just to run a straight sort for each, but run them in reverse order (tertiary sort, followed by secondary sort, followed by primary sort). This will have the net effect of the outcome you're wanting, and doesn't require each sort to take any notice of the other sorts, but produces the sort on the primary key, sub-ordered by the secondary key, sub-ordered again by the tertiary key. But, there may be more efficient implementations of the sort again that the database is able to employ. The good news is you don't need to know how it works—it just does. :)