Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用领域按多个属性排序_Ios_Swift_Realm - Fatal编程技术网

Ios 使用领域按多个属性排序

Ios 使用领域按多个属性排序,ios,swift,realm,Ios,Swift,Realm,如何使用多个属性对领域结果进行排序 我首先使用如下属性对它们进行排序: allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true) allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true).sortedResultsUsingProperty("timeStart", a

如何使用多个属性对领域结果进行排序

我首先使用如下属性对它们进行排序:

allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true)
allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true).sortedResultsUsingProperty("timeStart", ascending: true)
let sortProperties = [RLMSortDescriptor(property: "dateStart", ascending: true), RLMSortDescriptor(property: "timeStart", ascending: true)]
allShowsByDate = Show.allObjects().sortedResultsUsingDescriptors(sortProperties)
let sortProperties = [SortDescriptor(property: "dateStart", ascending: true), SortDescriptor(property: "timeStart", ascending: true)]
allShowsByDate = Realm().objects(MyObjectType).sorted(sortProperties)
但现在我还想通过另一个属性“timeStart”进行二次排序。我试着这样做:

allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true)
allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true).sortedResultsUsingProperty("timeStart", ascending: true)
let sortProperties = [RLMSortDescriptor(property: "dateStart", ascending: true), RLMSortDescriptor(property: "timeStart", ascending: true)]
allShowsByDate = Show.allObjects().sortedResultsUsingDescriptors(sortProperties)
let sortProperties = [SortDescriptor(property: "dateStart", ascending: true), SortDescriptor(property: "timeStart", ascending: true)]
allShowsByDate = Realm().objects(MyObjectType).sorted(sortProperties)

这只会使结果仅按第二个属性排序。请帮忙。

我是这样想的:

allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true)
allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true).sortedResultsUsingProperty("timeStart", ascending: true)
let sortProperties = [RLMSortDescriptor(property: "dateStart", ascending: true), RLMSortDescriptor(property: "timeStart", ascending: true)]
allShowsByDate = Show.allObjects().sortedResultsUsingDescriptors(sortProperties)
let sortProperties = [SortDescriptor(property: "dateStart", ascending: true), SortDescriptor(property: "timeStart", ascending: true)]
allShowsByDate = Realm().objects(MyObjectType).sorted(sortProperties)

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

allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true)
allShows = Show.allObjects().sortedResultsUsingProperty("dateStart", ascending: true).sortedResultsUsingProperty("timeStart", ascending: true)
let sortProperties = [RLMSortDescriptor(property: "dateStart", ascending: true), RLMSortDescriptor(property: "timeStart", ascending: true)]
allShowsByDate = Show.allObjects().sortedResultsUsingDescriptors(sortProperties)
let sortProperties = [SortDescriptor(property: "dateStart", ascending: true), SortDescriptor(property: "timeStart", ascending: true)]
allShowsByDate = Realm().objects(MyObjectType).sorted(sortProperties)

如果要使用更多属性,可以向数组中添加
SortDescriptor()
的值。

我找到了一个解决方案

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)

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

这是从领域2.5开始的方法

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

更新了Swift 4语法

let sortProperties = [SortDescriptor(keyPath: "queue"), SortDescriptor(keyPath: "name")]
let dogList = realm.objects(Dog.self).sorted(by: sortProperties)

看起来他们将RLMSortDescriptor重构为SortDescriptor知道如何在RealmSwift中实现吗?如果属性是对象怎么办?如何使用自定义函数进行多重排序?@TomSawyer您可以使用此方法sort({(first,second)->Bool in if first.startDate.compare(second.startDate)=.Ascending{return true}else{return false}}@AIG如果有嵌套的可选类型属性,我们将如何对其排序。在Swift 4中,您现在必须使用
.sorted(by:sortProperties)
。如果我放了多个排序描述,这也不会起作用。我真的不明白为什么还要放。如果域对象模型给了我们类似Dog.fields.queue.name等的东西,以避免“严格”的代码,这是有意义的,因为域是定义的。