Sort results from QML api



  • Hello,

    How to sort results from a query ?

    For example if I need to sort those todo's by "createdAt" date in descending order.
    @EnginioModel {
    id: enginioModel
    enginio: Enginio {
    backendId: AppConfig.backendData.id
    backendSecret: AppConfig.backendData.secret
    }
    query: {"objectType": "objects.todos" }
    }@

    Thanks



  • Hi,

    The query has a sort parameter which is a json array of objects to sort by.
    For example your query would look like:

    @
    query: {
    "objectType": "objects.todos"
    "sort" : [ {"sortBy": "createdAt", "direction": "desc"} ]
    }
    @

    You can find some documentation on sorting at https://engin.io/documentation/rest/parameters/sort



  • Great, thank you.

    Another observation is when you add a new object to the EnginioModel, it is appended at the end and not sorted accordingly to the "sort" condition unless you redo the full query.



  • This is a known shortcoming of the model that the planned notification support might be able to fix in some degree.

    Since the sorting criteria can be really complex keeping the model sorted after modifications is not trivial, and is best done by the user who knows the data in most cases it is probably better to use QSortFilterProxyModel and do the (re)sorting on the client side.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.