Dyrected
Deliver ContentTyped SDK

Sort

Order the documents a find returns by one field or several, ascending or descending.

By default, a find returns the newest documents first — Dyrected sorts by createdAt descending until you say otherwise. The moment you want a different order (alphabetical, highest price, most recent publish date), pass a sort string.

The shape of a sort string

sort is a single string naming the field to order by. A leading - flips it to descending:

// Oldest first (ascending)
await client.collection('posts').find({ sort: 'createdAt' })

// Newest first (descending)
await client.collection('posts').find({ sort: '-createdAt' })

Ascending is the default, so 'title' sorts A→Z and '-title' sorts Z→A. If you prefer it spelled out, ASC and DESC suffixes work too ('title DESC' is the same as '-title').

Sorting by more than one field

Separate fields with a comma. Dyrected sorts by the first field, then uses the next as a tie-breaker, and so on:

// Highest priority first; within the same priority, newest first
await client.collection('posts').find({ sort: 'priority,-createdAt' })

This is a comma-separated string, not an array — the SDK's sort option is always a string.

The chained form

Every read also exposes a fluent builder, so .sort() is equivalent to passing the option:

await client.collection('posts').find().sort('-createdAt')

Both styles hit the same query. Use whichever reads better next to the rest of your call.

What you can and can't sort by

Sorting happens in the database, on a single stored field at a time:

  • Sort by any top-level field your documents store — title, views, publishedAt, and so on.
  • You can't sort on a nested path like address.city or on a related document's fields. A sort value only accepts a flat field name; anything with a dot in it is ignored and Dyrected falls back to the default createdAt order.

If an order matters for how a page reads — a blog index, a leaderboard, a price list — set sort explicitly rather than relying on the default. To narrow which documents come back before you order them, reach for filter; to control how many you get per request, see pagination.

On this page

Dyrected| Cloud

Get your backend ready in minutes

Use a managed database, storage, APIs, and admin dashboard without setting up the infrastructure yourself.

Set Up My Backend