如何使用多个属性来排序我的 Realm 结果?
我首先使用这样的一个属性对它们进行排序:
allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true)
但现在我还想通过另一个属性“timeStart”进行二次排序。我试过这样:
allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true).sortedResultsUsingProperty("timeStart", ascending: true)
这只会使结果仅按第二个属性排序。请帮忙。
最佳答案
在 RealmSwift 中,我们可以像这样编写多个属性:
let sortProperties = [SortDescriptor(property: "dateStart", ascending: true), SortDescriptor(property: "timeStart", ascending: true)]
allShowsByDate = Realm().objects(MyObjectType).sorted(sortProperties)
如果你想使用更多的属性,你可以将SortDescriptor()
的值添加到数组中。
关于ios - 使用 Realm 按多个属性排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27365809/