Configure a default sort to apply when a read action or a has_many relationship has no sort.
Add ash_default_sort to your list of dependencies in mix.exs:
defdepsdo[{:ash_default_sort,"~> 0.2.0"}]enddefmodulePostdouseAsh.Resource,data_layer: Ash.DataLayer.Postgres,extensions: [AshDefaultSort]relationshipsdohas_many:comments,Commenthas_many:tags,Tagdosort[id: :asc]endendactionsdoread:readdoprimary?trueendread:read_sorteddopreparebuild(sort: [id: :desc])endread:read_alldoendread:read_everydoendenddefault_sortdosort[like_count: :desc,created_at: :desc]has_many_sort[id: :desc]include_primary_read?falseexcept[:read_all]endendIn the example above, [like_count: :desc, created_at: :desc] is applied to read_every.
This is because read is excluded by include_primary_read? false, read_sorted is not affected because it already includes a sort, and read_all is excluded by the except option.
And the default_sort for comments is set to [id: :desc] because has_many_sort is present, and comments has no sort or default_sort options.
MIT