Sortable properties¶
What this does¶
Sortable = true on [Map] opts the property into the sort array of an incoming FilterRequest. The generator emits a typed OrderBy / ThenBy chain dispatch for that property. DefaultSortDirection = SortDir.Desc flips the default direction for SortItem entries that omit Direction.
When to use¶
Any property your UI exposes as a sortable column. The sort field of a request is an ordered array, so a multi-key sort like "by department, then by created-at descending" requires marking each contributing column sortable.
Minimal code¶
[GenerateFilter<User>]
public partial class UserFilter
{
[Map(nameof(User.Id), Sortable = true)]
private static partial void MapId();
// DefaultSortDirection.Desc — a SortItem with no Direction lands newest-first.
[Map(nameof(User.CreatedAt), Sortable = true, DefaultSortDirection = SortDir.Desc)]
private static partial void MapCreatedAt();
[Map(nameof(User.Age), Sortable = true, DefaultSortDirection = SortDir.Desc)]
private static partial void MapAge();
}
A consumer requesting { "sort": [{ "field": "createdAt" }, { "field": "id", "direction": "asc" }] } gets OrderByDescending(u => u.CreatedAt).ThenBy(u => u.Id).
Variations¶
- Multiple sortable properties — every
[Map]withSortable = truebecomes a tie-breaker option in thesortarray. - Explicit per-
SortItemdirection —{ "field": "age", "direction": "asc" }overrides the property'sDefaultSortDirection. - Combine with
Alias = "..."— thesort[].fieldmatches the alias, not the underlying property name.
Pitfalls¶
- Only one
[Map]per property is allowed to setSortable = true. Repeating it on multiple methods triggersFN0002. - Properties with sortable-looking CLR types (numbers, dates) that are not marked
Sortable = trueraiseFN1002as a friendly nudge.