API request with multiple sort params

Hi,

I migrate data from a source to CockpitCMS. The import and mapping function in Cockpit is really nice, but unfortunately the collection-entry-properties _created and _modified can not be mapped with properties from the imported json :frowning:

So I decided to have a additional field in my collection: “date”. Users should handle this as optional, but the imported json datas “date” I will map to the new field.

I want the API request now to sort the results by two parameters: the new date field and _created.

If i write it in this way:

sort: {date: -1, _created: -1},

the response gives me first the entries which have a “date” field filled and then the left _created ones… But I want to mix the results.

“Sort by date and if there is now date setted sort by _created”

or is there a way to map _created and _modified while importing json?

Use the cli instead of the ui, see: _id regenerating when import entries to collection · Issue #851 · agentejo/cockpit · GitHub

You could hook into the collections.find.after event, merge the dates and return the result

if (COCKPIT_API_REQUEST) {
    $app->on('collections.find.after', function($name, &$entries) {
        // merge/sort dates
    });
}

edit: The custom sorting on the after event won’t work with filters because you can only sort/merge found entries.