Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order by multiple properties using Realm

Tags:

ios

swift

realm

How can I order my Realm results using multiple properties?

I'm sorting them first using one property like this:

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

But now I also want to do a secondary sort by another property "timeStart". I tried like this:

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

This will just make the results sorted only by the second property. Please help.

like image 986
codeman Avatar asked Dec 08 '14 19:12

codeman


1 Answers

In RealmSwift we can write multiple properties like this:

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

If you want to use more properties,you can add values of SortDescriptor() to the array.

like image 97
Yoshitaka Avatar answered Oct 09 '22 22:10

Yoshitaka