NSSortDescriptor具有一个功能 [英] NSSortDescriptor with a function

查看:117
本文介绍了NSSortDescriptor具有一个功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用NSSortDescriptor方面经验有限。
它是在一个键上排序,它工作正常。
但这是我现在需要的,我有一组数字,例如:
{(2,3),(44,5),(6,17),(33, 7)......(173,21)}
我想根据给定函数myfunction(x,y)的值对对(x,y)进行排序。
有一个简单的想法是制作三元组(x,y,z),其中z将是myfunction(x,y)的计算,然后对三元组进行排序,但这不是我想要的。

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.

是否有正确的方法使用NSSortDescriptor来完成我需要的工作?

Is there a proper way to use NSSortDescriptor to do what I need?

感谢您提供任何信息。

推荐答案

除非您使用SQLite存储将此排序描述符与Core Data一起使用,否则您可以在属性表示z的对象上创建瞬态属性,你的功能的计算。然后,您可以对该瞬态属性进行排序,它将生成您想要的结果,而不存储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.

但是,如果您将Core Data与商店一样使用SQLite,其中整个存储内容读入内存,您不能使用瞬态属性进行排序,您必须将z值实际存储在托管对象中才能实现您所描述的内容,或者将所有托管对象读入一个数组,然后在瞬态属性上对该对象数组进行排序...如果您只有有限数量的托管对象,那么这样就行了,但不是那么好。

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.

这篇关于NSSortDescriptor具有一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆