docs/docs/en/data-sources/field-sort/index.md
In NocoBase, the Sort field records the sort value of collection records. It is commonly used for drag-and-drop ordering in blocks such as Tables and Kanban.
Sort fields support ungrouped sorting and grouped sorting. Grouped sorting is suitable when records must be ordered independently inside each group, such as students ordered by class or tasks ordered by Kanban status.
:::warning Note
Because a Sort field is a field in the same collection, grouped sorting does not support the same record appearing in multiple groups at the same time.
:::
The Sort field is provided by a built-in plugin and does not require separate installation.
On the collection's Configure fields page, click Add field and select Sort.
When creating a Sort field, NocoBase initializes sort values:
:::warning Note
If sort-value initialization fails during field creation, the Sort field is not created. Within a range, when one record moves from position A to B, the sort values of all records between A and B change. If updating any one record fails, the move fails and none of the related sort values change.
:::
The following example creates a sort1 field without grouped sorting.
Each record's Sort field is initialized according to its primary key and Created at field.
The following example creates a sort2 field grouped by Class ID.
All records in the collection are first grouped by Class ID, then their Sort field values are initialized.
Sort fields are primarily used for drag-and-drop ordering of records in blocks. Table and Kanban blocks currently support drag-and-drop sorting.
:::warning Note
:::
Table blocks can use Sort fields to adjust record order by drag and drop.
Relation Table blocks can also use Sort fields for drag-and-drop ordering.
<video controls width="100%" src="https://static-docs.nocobase.com/20240409111903_rec_.mp4" title="Drag-and-drop sorting in a relation Table block"></video>
:::warning Note
In a one-to-many relation block, when an ungrouped Sort field is selected, all records can participate in sorting. When records are grouped by the foreign key before sorting, the rule affects only data in the current group. The final display can look the same, but the set of records participating in the sort differs.
:::
Kanban blocks can use Sort fields to adjust card order by drag and drop.
Suppose records are ordered as follows:
[1,2,3,4,5,6,7,8,9]
When 5 moves forward to the position of 3, only the positions of 3, 4, and 5 change. Record 5 takes the position of 3, and 3 and 4 each move one position backward.
[1,2,5,3,4,6,7,8,9]
Then move 6 backward to the position of 8. Record 6 takes the position of 8, and 7 and 8 each move one position forward.
[1,2,5,3,4,7,8,6,9]
With grouped sorting, when a record moves to another group, its own group also changes. Suppose there are two groups:
A: [1,2,3,4]
B: [5,6,7,8]
When 1 moves after 6, its group changes from A to B.
A: [2,3,4]
B: [5,6,1,7,8]
Suppose records are ordered as follows:
[1,2,3,4,5,6,7,8,9]
The interface displays only:
[1,5,9]
When 1 moves to the position of 9, the positions of 2, 3, 4, 5, 6, 7, and 8 also change.
[2,3,4,5,6,7,8,9,1]
The interface finally displays:
[5,9,1]