具有函数的 NSSortDescriptor
我在使用 NSSortDescriptor 方面的经验有限。 它可以通过一键进行排序,而且效果很好。 但这是我现在需要的,我有一组数字对,例如: {(2,3),(44,5),(6,17),(33,7)……(173,21)} 我想根据给定函数 myfunction(x,y) 的值对 (x,y) 进行排序。 有一个简单的想法,即制作三元组 (x,y,z),其中 z 是 myfunction(x,y) 的计算,然后对三元组集进行排序,但这不是我想要的。
有没有正确的方法使用 NSSortDescriptor 来做我需要的事情?
感谢您提供任何信息。
I only have a limited experience in using NSSortDescriptor.
It was sorting on one key and it worked fine.
But here is what I need now, I have a set of pairs of numbers, for example :
{(2,3), (44,5), (6,17), (33,7) ……(173,21)}
I want to sort the pairs (x,y) according to the value of a given function myfunction(x,y).
There is the trivial idea of making triplets (x,y,z) where z would be the computation of myfunction(x,y) and then sort the set of triplets, but this not what I want.
Is there a proper way to use NSSortDescriptor to do what I need?
Thanks for any information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您使用 SQLite 存储将这种排序描述符与 Core Data 结合使用,否则您可以在对象上创建一个瞬态属性,其中该属性表示 z(函数的计算)。然后,您可以对该瞬态属性进行排序,它将产生您想要的结果,而不存储 z 值。
但是,如果您将 Core Data 与 SQLite 之类的存储结合使用,其中整个存储内容未读入内存,则无法使用瞬态属性进行排序,并且必须实际存储 z 值在托管对象中以实现您所描述的目的,或者将所有托管对象读入一个数组,然后对该瞬态属性上的对象数组进行排序...如果您只有有限数量的对象,那么这将可以正常工作管理对象,但其他方面则不太好。
Unless you are using this sort descriptor with Core Data using a SQLite store, you could create a transient attribute on the object where the attribute represented z, the computation of your function. You could then sort on that transient attribute and it would produce the results you want, without storing the z values.
However, if you are using Core Data with a store like SQLite where the entire store contents are not read into memory, you cannot use transient attributes to sort on and you would have to either store your z values actually in the managed objects in order to achieve what you are describing, or read all of your managed objects into an array, and then sort that array of objects on the transient property... which would work OK if you only had a limited number of managed objects, but not so well otherwise.