Reputation: 1683
This part of code:
var posts: Results<Post> {
get {
return realm.objects(Post.self).sorted(byKeyPath: "id", ascending: false)
}
}
returns ids like
96, 950, 94, 930
My post id is String
. How to get right sorted objects without making id Int
.
Upvotes: 0
Views: 117
Reputation: 15991
Unfortunately, when sorting via a String
property, Realm automatically sorts it alphabetically, instead of numerical sorting.
Adding the ability to manually sort properties via a block/closure is on the roadmap, but there's nothing to announce for it just yet. I recommend you go and '+1' that particular GitHub thread so it can get more priority. :)
The most ideal solution would be to change id
to an Int
property if you can. Failing that, njzk2's solution of manually sorting the Results
into a Swift array 'will' work, but that will page all of the Realm objects into memory at once (causing more overhead), so it's not optimal.
Upvotes: 1