How to complete the challenge GET /api/todos (200) ? _sortBy multiple

How to issue a GET request on a top level entity endpoint and sort the returned todos by more than one field.

GET /api/todos (200) ? _sortBy multiple

Issue a GET request on the /api/todos end point with a query parameter to sort todos by multiple fields, requesting the response in JSON format.

  • _sortBy is the URL parameter used to sort collection results
  • separate multiple sort fields with commas
  • a plain field name sorts ascending
  • prefix a field with - to sort descending
  • for example, _sortBy=doneStatus,-id sorts by doneStatus ascending, then by id descending within each done status
  • add header Accept: application/json if not present so the filtered and sorted todo collection is returned as JSON

Basic Instructions

  • Issue a GET request to end point "/api/todos"
    • https://apichallenges.com/api/todos
  • The request should have an X-CHALLENGER header to track challenge completion
  • The request should have an Accept: application/json header so the API returns todos in JSON format
  • Add _sortBy=doneStatus,-id as a URL parameter:
    • https://apichallenges.com/api/todos?_sortBy=doneStatus,-id
  • The response status code should be 200 because the request is accepted
  • Check that todos are grouped by doneStatus, and within each group the ids are descending

Try it now

GET /api/todos?_sortBy=doneStatus,-id to sort todos by multiple fields

Example Request

> GET /api/todos?_sortBy=doneStatus,-id HTTP/1.1
> Host: apichallenges.com
> User-Agent: rest-client
> X-CHALLENGER: x-challenger-guid
> Accept: application/json

Example Response

< HTTP/1.1 200 OK
< Connection: close
< Content-Type: application/json
< X-Challenger: x-challenger-guid

Returned body:

{
  "todos": [
    {
      "id": 8,
      "title": "not done with higher id",
      "doneStatus": false,
      "description": ""
    },
    {
      "id": 3,
      "title": "not done with lower id",
      "doneStatus": false,
      "description": ""
    },
    {
      "id": 9,
      "title": "done with higher id",
      "doneStatus": true,
      "description": ""
    }
  ]
}

Lessons Learned

  • Multiple _sortBy fields define tie-breaking when the first field has duplicate values.
  • Multi-sort tests need data with shared primary values, otherwise the secondary sort is invisible.
  • Comma-separated sort criteria should be parsed in the order supplied.

Suggested Experiments

  • Create several todos with the same doneStatus but different title values and sort by doneStatus,+title.
  • Reverse only the secondary sort field and check whether records inside each primary group change order.
Experiment with this endpoint