Search code examples
sortingreact-nativerealm

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"

Solution

  • 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.