brother
brother

Reputation: 8171

Custom orderBy with odata?

In an AngularApp, i am getting my data from a WebAPI via OData queries.

All results have a date and an int statuscode. Statuscode can be 1, 2, 3 or 4.

I want to order my results, so all results with statuscode 1, goes to the top of the list. The rest of the results, with code 2, 3 and 4, gets sorted by the data attached to them, and not the statuscode.

Possible?

This is my code as it looks now. It just sorts by the date, and nothing else.

return Products.query({ $orderby: 'Date' });

Upvotes: 1

Views: 3108

Answers (1)

lencharest
lencharest

Reputation: 2935

From your description, it sounds like you want all Products with StatusCode of 1 to be treated equally. That is, they are not further sorted by Date. If you can relax this requirement a bit and allow all Products to be sorted by StatusCode and Date, there is a straightforward solution:

return Products.query({ $orderby: 'StatusCode,Date' });

The $orderby query option in OData v4 consists of "a comma-separated list of expressions whose primitive result values are used to sort the items". Property names are the most common expressions used to produce sort values.

If you want to reverse the ordering on a particular property, use the desc suffix on that property like so:

return Products.query({ $orderby: 'StatusCode,Date desc' });

Upvotes: 1

Related Questions