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

How to issue a GET request on a top level entity endpoint and sort the returned todos in ascending order.

GET /api/todos (200) ? _sortBy ascending

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

  • GET request will return items from the /api/todos end point
  • 200 is the success code meaning the request was accepted
  • _sortBy is the URL parameter used to sort collection results
  • use a todo field name as the value, e.g. _sortBy=title
  • a plain field name sorts ascending
  • +field also means ascending, but using just the field name avoids client URL encoding surprises
  • ensure header Accept: application/json exists so the filtered and sorted todo collection is visible 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=title as a URL parameter:
    • https://apichallenges.com/api/todos?_sortBy=title
  • The response status code should be 200 because the request is accepted
  • Check that the returned todos are ordered by title from A to Z

Try it now

GET /api/todos?_sortBy=title to sort todos by title

Example Request

> GET /api/todos?_sortBy=title 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": 4,
      "title": "file paperwork",
      "doneStatus": false,
      "description": ""
    },
    {
      "id": 2,
      "title": "process payroll",
      "doneStatus": true,
      "description": ""
    }
  ]
}

Lessons Learned

  • _sortBy=+id makes ascending order explicit instead of relying on a default.
  • Sorting tests should compare adjacent values, not just the first and last record.
  • A stable sort gives clients predictable collection browsing.

Suggested Experiments

  • Sort by +title and compare alphabetic order with +id.
  • Add a new todo and confirm its position is based on the sorted field, not creation time alone.
Experiment with this endpoint