# How to sort included resources?

**URL:** <https://discuss.jsonapi.org/t/how-to-sort-included-resources/198>\
**Category:** Uncategorized\
**Created:** [November 12, 2015, 6:23pm UTC](https://discuss.jsonapi.org/t/how-to-sort-included-resources/198 "2015-11-12T18:23:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![blukes](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@blukes](https://discuss.jsonapi.org/u/blukes)\
**Post date:** [November 12, 2015, 6:23pm UTC](https://discuss.jsonapi.org/t/how-to-sort-included-resources/198/1 "2015-11-12T18:23:01Z")

</div>

What is the best way to to go about sorting included resources?

If hitting the endpoint `/allTheObjects?sort=-name&include=theOtherObjects`, how would we apply a sort of `createDate` to the collection of `theOtherObjects` ?

Thanks very much for any input and advice.

---

<div class="post-metadata">

**Author:** ![ethanresnick](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@ethanresnick](https://discuss.jsonapi.org/u/ethanresnick)\
**Post date:** [November 13, 2015, 6:42pm UTC](https://discuss.jsonapi.org/t/how-to-sort-included-resources/198/2 "2015-11-13T18:42:44Z")

</div>

The `"included"` collection can come from many different relationship paths and resources, so it usually doesn’t make sense to sort it, and there’s no built-in way to doing so. However, JSON API does let you define query parameters unique to your API, so you could easily define one that performs this feature.

API-specific query parameters have to include at least one non–[a-z] character, so that JSON API can define new query parameters in the future without the risk of them clobbering your own. And a query parameter can’t do anything that violates a requirement of the base spec, but sorting `"included"` is definitely allowed.

So, I’d define a parameter called `sort-included` and have it use the same syntax as the standard sort parameter, and that should do it!

---

<div class="post-metadata">

**Author:** ![blukes](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@blukes](https://discuss.jsonapi.org/u/blukes)\
**Post date:** [November 13, 2015, 6:45pm UTC](https://discuss.jsonapi.org/t/how-to-sort-included-resources/198/3 "2015-11-13T18:45:11Z")

</div>

Thanks so much for the reply, I’ll give that a try!

---

<div class="post-metadata">

**Author:** ![andrewhenderson](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/andrewhenderson/32/115_2.png) [@andrewhenderson](https://discuss.jsonapi.org/u/andrewhenderson)\
**Post date:** [November 13, 2015, 7:38pm UTC](https://discuss.jsonapi.org/t/how-to-sort-included-resources/198/4 "2015-11-13T19:38:58Z")

</div>

@blukes You might consider using multiple “?” in the URI which is allowed: [http://stackoverflow.com/a/2924187/933711](http://stackoverflow.com/a/2924187/933711)

```
/allTheObjects?sort=-name&include=theOtherObjects?sort=-date,evenMoreObjects?sort=+amount

```

This way you could sort each included set by specific parameters.

@ethanresnick Chime in if you have any thoughts on this approach.

---

<div class="post-metadata">

**Author:** ![ethanresnick](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@ethanresnick](https://discuss.jsonapi.org/u/ethanresnick)\
**Post date:** [November 13, 2015, 8:16pm UTC](https://discuss.jsonapi.org/t/how-to-sort-included-resources/198/5 "2015-11-13T20:16:45Z")

</div>

@andrewhenderson The feature you’re proposing could definitely be useful, and you’re right that having multiple question marks is valid in a URI’s query string. But having question marks as part of the `?include` parameter’s value in JSON API is not. From the spec:

> The value of the include parameter MUST be a comma-separated (U+002C COMMA, “,”) list of relationship paths. A relationship path is a dot-separated (U+002E FULL-STOP, “.”) list of relationship names.

Also, the same resource can be linked from multiple relationships, but it’ll only show up in `"included"` once (that’s the point), so you’d have to define how to handle this case. For example, imagine a request like `GET /articles?include=author,editor` where the same person is sometimes an author and sometimes an editor; which rules should this person be sorted under?

But I guess the bottom line here is that the spec gives each API total freedom to define whatever query parameter(s) it wants (as long as they contain a non-a to z character) to sort the included collection however it wants.
