View Source Ash.Sort (ash v2.11.8)

Utilities and types for sorting.

important

Important

Keyset pagination cannot currently be used in conjunction with aggregate and calculation sorting. Combining them will result in an error on the query.

Link to this section Summary

Functions

A utility for parsing sorts provided from external input. Only allows sorting on public attributes and aggregates.

Reverses an Ash sort statement.

A utility for sorting a list of records at runtime.

Link to this section Types

@type sort_order() ::
  :asc
  | :desc
  | :asc_nils_first
  | :asc_nils_last
  | :desc_nils_first
  | :desc_nils_last
@type t() ::
  [
    atom()
    | {atom(), sort_order()}
    | {atom(), {sort_order(), Keyword.t() | map()}}
  ]
  | atom()

Link to this section Functions

Link to this function

parse_input!(resource, sort, handler \\ nil)

View Source

Same as parse_input/2 except raises any errors

See parse_input/2 for more.

Link to this function

parse_input(resource, sort, handler \\ nil)

View Source
@spec parse_input(
  Ash.Resource.t(),
  String.t()
  | [atom() | String.t() | {atom(), sort_order()} | [String.t()]]
  | nil,
  nil | (String.t() -> nil | atom() | {atom(), map()})
) :: {:ok, t()} | {:error, term()}

A utility for parsing sorts provided from external input. Only allows sorting on public attributes and aggregates.

The supported formats are:

sort-strings

Sort Strings

A comma separated list of fields to sort on, each with an optional prefix.

The prefixes are:

  • "+" - Same as no prefix. Sorts :asc.
  • "++" - Sorts :asc_nils_first
  • "-" - Sorts :desc
  • "--" - Sorts :desc_nils_last

For example

"foo,-bar,++baz,--buz"

a-list-of-sort-strings

A list of sort strings

Same prefix rules as above, but provided as a list.

For example:

["foo", "-bar", "++baz", "--buz"]

handling-specific-values

Handling specific values

A handler function may be provided that takes a string, and returns the relevant sort It won't be given any prefixes, only the field. This allows for things like parsing the calculation values out of the sort, or setting calculation values if they are not included in the sort string.

To return calculation parameters, return {:field, %{param: :value}}. This will end up as something like {:field, {:desc, %{param: :value}}}, with the corresponding sort order.

This handler function will only be called if you pass in a string or list of strings for the sort. Atoms will be assumed to have already been handled. The handler should return nil if it is not handling the given field.

Link to this function

parse_sort(resource, sort, handler \\ nil)

View Source

Reverses an Ash sort statement.

Link to this function

runtime_sort(results, sort, api \\ nil)

View Source

A utility for sorting a list of records at runtime.

For example:

Ash.Sort.runtime_sort([record1, record2, record3], name: :asc, type: :desc_nils_last)

Keep in mind that it is unrealistic to expect this runtime sort to always be exactly the same as a sort that may have been applied by your data layer. This is especially true for strings. For example, Postgres strings have a collation that affects their sorting, making it unpredictable from the perspective of a tool using the database: https://www.postgresql.org/docs/current/collation.html