Realm-js: Result.sorted reverse property isn't working

Created on 4 Mar 2016  路  11Comments  路  Source: realm/realm-js

I'm trying to sort my ListView by date with the newest first. The documentation says .sorted takes a reverse param, but it isn't making any difference.

My code:

let items = this.db.objects(ITEMS_TABLE);
items.sorted('dateUpdated', true);

I get the same sort order with items.sorted('dateUpdated', true); and items.sorted('dateUpdated', false);

This is with Realm 0.10.0

Most helpful comment

@asamiller sorted() returns a new Results instance, so you probably need to change to something like this:

let items = this.db.objects(ITEMS_TABLE).sorted('dateUpdated', true);

All 11 comments

Can you give enough code to reproduce the issue? Our test for this passes without issue but perhaps this bug only manifests itself in certain situations. If you could share your full schema for ITEMS_TABLE it might be helpful.

I've just tested all the property type, forward/reverse combinations and everything seems to work https://github.com/realm/realm-js/commit/621ee36fada20426c68c84d82f07f71df73488a9

@asamiller sorted() returns a new Results instance, so you probably need to change to something like this:

let items = this.db.objects(ITEMS_TABLE).sorted('dateUpdated', true);

Good catch - in the beta the sort api used to change the sort order on the existing results, but was changed to return a new results for the 0.10.0 release. I assume this fixes the issue?

Yes, thanks! That was the issue. I had been using it the other way from the beta.

There isn't any mention of this in the current documentation.

Is there a place where I can issue a PR for those docs?

Thanks.

@joshuapinter You're right that we probably should mention it there. The API documentation for sorted covers this, so maybe we should provide a link to that documentation on that page as well.

@appden Damn, didn't even know these API docs existed - they're much better! A link might help others for sure.

Flash forward a year later and I stumbled on the same problem. The Realm JS documentation should have mentioned in more details on how to sort properly in reverse order.

In order to perform sorting in reverse order, the .sorted() method should be invoked via fluent API chaining without intermediary variable assignment

Improper reverse sorting:

let items = realm.objects('Article').filtered('createdAt > $0', twoDaysAgo);
const reverseSortedItems = items.sorted('createdAt', true); //will not work
console.log(reverseSortedItems);

Proper reverse sorting:

const sortedItems = realm.objects('Article').filtered('createdAt > $0', twoDaysAgo).sorted('createdAt', true); //sorted in reverse order by date
console.log(sortedItems)

@mikaelfs Just refreshing my memory on this, I didn't realize you couldn't use an intermediary variable assignment. Good catch.

That really shouldn't be like that and if it is then it should be clearly mentioned in the docs.

Thanks @mikaelfs, that was exatcly what I was trying to figure. Examples like this in the docs would be very useful.

Was this page helpful?
0 / 5 - 0 ratings