如何根据数组顺序使NSSortDeScriptor进行排序?

发布于 2025-02-04 08:13:00 字数 568 浏览 2 评论 0原文

我有一个对象ID数组,并且数组具有正确的顺序。

let objectsIDs = [504, 316, 195, ...]

而且我正在使用此nsfetchedResultSconTroller使用nspredicate

NSPredicate(format: "%K IN %@", #keyPath(object.id), objectsIDs)

nsfetchedResultscontroller需要nssortdescriptor,我无法使用 contect 代码>或ID属性对象进行分类。

如何获得nssortDescriptor根据objectsids数组顺序对对象进行排序? objectsids数组始终具有正确的顺序,nssortDescriptor必须取决于其顺序。

I have an array of object IDs and the array has the correct order.

let objectsIDs = [504, 316, 195, ...]

And I am using NSFetchedResultsController with this NSPredicate:

NSPredicate(format: "%K IN %@", #keyPath(object.id), objectsIDs)

NSFetchedResultsController requires NSSortDescriptor and I cannot use createdAt or id property to sort the objects.

How can I get NSSortDescriptor to sort the objects based on the objectsIDs array order? The objectsIDs array always have the correct order and NSSortDescriptor must depend on its order.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼痛 2025-02-11 08:13:00

如何使nssortDeScriptor根据objectsids数组顺序排序对象?

你不能。您要做的是 cosort 数组,即按照另一个数组的顺序对其进行排序。 NSSORTDESCRIPTOR不这样做。 您必须这样做。合作社是一个经过充分讨论的问题。

How can I get NSSortDescriptor to sort the objects based on the objectsIDs array order?

You can't. What you're trying to do is cosort the array, ie sort it according to the order of another array. NSSortDescriptor doesn't do that. You have to do it. Cosorting is a well discussed problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文