通过使用Realm的多个属性进行sorting

我如何使用多个属性来订购我的Realm结果?

我首先使用一个属性来sorting它们:

allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true) 

但是现在我也想通过另一个属性“timeStart”进行二次sorting。 我试过这样的:

 allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true).sortedResultsUsingProperty("timeStart", ascending: true) 

这只会使结果仅由第二个属性sorting。 请帮忙。

在RealmSwift中,我们可以编写如下的多个属性:

 let sortProperties = [SortDescriptor(property: "dateStart", ascending: true), SortDescriptor(property: "timeStart", ascending: true)] allShowsByDate = Realm().objects(MyObjectType).sorted(sortProperties) 

如果您想使用更多的属性,可以将SortDescriptor()值添加到数组中。

如下图所示:

 let sortProperties = [RLMSortDescriptor(property: "dateStart", ascending: true), RLMSortDescriptor(property: "timeStart", ascending: true)] allShowsByDate = Show.allObjects().sortedResultsUsingDescriptors(sortProperties) 

我find了一个解决scheme。

 var dataSource: Results<DLVCasting>! = nil let realm = try! Realm() let sortDescriptors = [SortDescriptor(property: "someValue", ascending: false)] dataSource = realm.objects(MyClass.self).sorted(sortDescriptors); dataSource = dataSource.sorted("anotherValue", ascending: false) 

但是,如果你在数组中放置多个sorting描述符,如下面的例子

 let sortDescriptors = [SortDescriptor(property: "someValue", ascending: false),SortDescriptor(property: "someValue", ascending: false)] 

这是行不通的。 我真的不明白为什么。

这是如何做到这一点的领域2.5

  dataArray = try! Realm().objects(Book.self) .sorted( by: [SortDescriptor(keyPath: "Author", ascending: true), SortDescriptor(keyPath: "Title", ascending: true)] )