Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm React native sorted order

Basically I have a MessageSchema with a date property, on my app I need to query all the messages stored on the database and be able to sort them by their date to display them on a ListView, I do the query as follows:

return realm.objects("Message").sorted('date');

This works, but only one way, the messages are sorted on ascending order only, I haven't found a way to do it on descending order and the docs of the react native only show one example:

let hondas = realm.objects('Car').filtered('make = "Honda"');
// Sort Hondas by mileage
let sortedHondas = hondas.sorted('miles');

Any advice is welcome.

Versions:

react-native: "0.40.0"
realm js: "1.0.2"
like image 581
vicvans20 Avatar asked Feb 17 '17 02:02

vicvans20


Video Answer


1 Answers

return realm.objects("Message").sorted('date', true);

Looking at the source code you can see that the sorted method expects a descriptor and a Boolean named reverse, that it is set as false unless you change it, so the code above just reverses the order.

like image 157
Hansel Wave Avatar answered Oct 06 '22 19:10

Hansel Wave