Skip to main content
Version: Next

Sorting and Adjustment

Files Referenced in this Document

  • IMyEntityNameAppService.cs
  • MyEntityNameAppService.cs

Table of Contents​

  1. Introduction
  2. Sorting Logic (SortAsync)
  3. Implementation Strategy
  4. Usage Scenarios

Introduction​

In many business applications, users need to define a specific order for displaying items, independent of creation time or alphabetical order. The SortAsync method provides a manual ordering capability, allowing users to drag and drop items in the UI and persist this order to the backend.

Sorting Logic (SortAsync)​

Method Signature: Task SortAsync(List<Guid> ids)

Description: Updates the display order of entities based on the sequence of IDs provided in the list.

Process:

  1. Receives an ordered list of entity IDs (List<Guid>).
  2. Iterates through the list, assigning a sequence number (index) to each entity.
  3. Updates the SortIndex (or equivalent) field of each entity in the database.
  4. Saves changes.

Implementation Strategy​

The typical implementation involves fetching the entities (or updating them directly) and setting a sort order field.

public async Task SortAsync(List<Guid> ids)
{
// Simplified logic
for (int i = 0; i < ids.Count; i++)
{
var id = ids[i];
var entity = await Repository.GetAsync(id);
entity.SortIndex = i; // Assuming SortIndex property exists
await Repository.UpdateAsync(entity);
}
}

Note: The actual implementation might be optimized to use batch updates or direct SQL execution for better performance.

Usage Scenarios​

  • Menu Ordering: Reordering menu items in a navigation bar.
  • Task Prioritization: Moving tasks up or down in a todo list.
  • Display Configuration: Customizing the order of widgets on a dashboard.

Request:

POST /api/app/my-entity-name/sort
[
"first-item-id",
"second-item-id",
"third-item-id"
]

The backend will update "first-item-id" to have index 0, "second-item-id" index 1, etc.