Skip to content

[DataGrid] Add DefaultSortPriority and leave the grid unsorted when the last sort level is removed - #5460

Open
Steven Rasmussen (StevenRasmussen) wants to merge 8 commits into
microsoft:devfrom
StevenRasmussen:feature/grid-default-sort
Open

Steven Rasmussen (StevenRasmussen) wants to merge 8 commits into
microsoft:devfrom
StevenRasmussen:feature/grid-default-sort

Conversation

@StevenRasmussen

@StevenRasmussen Steven Rasmussen (StevenRasmussen) commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request

📖 Description

Follow-up to the multi-column sorting work in #5312.

Add DefaultSortPriority to order the declared default sort

When more than one column declares IsDefaultSortColumn, the default sort follows the order the columns are declared in. This forces the most important sort column to also be the first column, which often isn't what the layout needs. It also gets in the way of grids that build their columns and default sort at runtime, for example from a saved view.

ColumnBase.DefaultSortPriority (int?, default null) sets that order:

  • Lower values sort first. Columns without a priority follow those with one, in declaration order, so existing grids behave exactly as before.
  • The priority only applies to columns that also set IsDefaultSortColumn.
  • In single sort mode, the grid is sorted by the declared column with the lowest priority.
  • The ordered sort is applied once the columns are collected, so the first data request (Items or ItemsProvider) already carries it. The grid doesn't load unsorted and then sort again.
  • ResetSortAsync, Shift+S and the header menu's Reset restore the sort in priority order.
  • A sort saved in the URL (SaveStateInUrl) still takes precedence over the declared sort.
<PropertyColumn Title="Full Name" Property="@(e => e.FullName)" Sortable="true"
                IsDefaultSortColumn="true" DefaultSortPriority="2" />
<PropertyColumn Title="Start Date" Property="@(e => e.StartDate)" Sortable="true"
                IsDefaultSortColumn="true" InitialSortDirection="DataGridSortDirection.Descending" DefaultSortPriority="1" />

Fix: removing the last sort level brought the default sort back

In multiple sort mode, removing the last remaining sort level restored the declared default sort. This happened with right-click on a header, Clear in the header menu and RemoveSortByColumnAsync. A user removing sorts one by one saw them reappear, and Clear did the opposite of what its label says. It only shows with a declared default sort, which the demo didn't have.

  • Removing the last level now leaves the grid unsorted, as Clear all and ClearSortAsync do. With SaveStateInUrl it is saved as unsorted (orderby=), so a reload doesn't bring the default sort back.
  • ResetSortAsync, Shift+S and Reset still return to the default sort.
  • The header menu used to offer Reset only when two or more columns were sorted, because Clear on the last level did the reset. It now offers Reset whenever a default sort is declared and the grid isn't sorted that way, including with one sorted column or none. Without this, there would be no menu option back to the default sort.

Behavior changes for existing grids

  • Multiple sort mode: removing the last sort level no longer restores the declared sort (the fix above).
  • Single sort mode: RemoveSortByColumnAsync only keeps the sort of the column the grid is actually sorted by by default. Before, any column marked IsDefaultSortColumn was kept, even one that wasn't the default in single mode, so removing its sort did nothing.

👩‍💻 Reviewer Notes

  • Priority ordering is in FluentDataGrid.RebuildDeclaredSort. It only sorts when a column sets a priority, so grids without priorities skip that work on every render. The declared sort is applied in FinishCollectingColumns, before the URL state is applied.
  • The last-level fix is in RemoveSortByColumnAsync. The Reset visibility uses the new internal IsSortedAsDeclared.

Smoke test: run the demo and open /DataGrid/MultiSort. The example now declares two default sort columns (Department, then Start Date newest first) and has a Reset sort button.

  1. Right-click the sorted headers one at a time. The grid ends unsorted.
  2. Tick Use menu for column actions and use Clear on each sorted column. Same result. Reset is offered once the sort differs from the default.
  3. Reset sort, the menu's Reset and Shift+S return to Department, then Start Date.

📑 Test Plan

New tests in FluentDataGridMultiSortTests.razor:

  • DefaultSortPriority:
    • The first data request is already in priority order, and the headers show the right sort level numbers and aria-sort.
    • Ordering covers no priority, mixed, equal, negative and int.MaxValue values.
    • In single mode, the lowest priority wins.
    • Reset and Shift+S restore in priority order.
    • A sort that can't be appended can lead when given the first priority.
    • A priority without IsDefaultSortColumn is ignored.
    • A URL sort still wins.
  • Single mode RemoveSortByColumnAsync only keeps the column the grid is actually sorted by by default.
  • Removing the last level:
    • Leaves the grid unsorted, also after a re-render, and saves it as unsorted in the URL.
    • Right-clicking each sorted header in turn leaves the grid unsorted with no sort indicators. This test fails against the previous code.
  • Header menu: Clear on the last sorted column leaves the grid unsorted and offers Reset, and Reset isn't offered while the grid has its default sort.
  • The test that expected the old restore-on-last-level behaviour was updated.

The full unit test suite passes, and the Release build (including net11.0 analyzers) has no warnings.

✅ Checklist

General

  • I have added tests for my changes.
  • I have tested my changes.
  • I have updated the project documentation to reflect my changes.
  • I have read the CONTRIBUTING documentation and followed the standards for this project.

Issue discussion declaration

Discuss changes before implementing them to confirm that the problem and proposed approach fit the project. See Discuss before implementing for the process and exceptions.

  • I have discussed this change in an issue and received maintainer agreement before implementation, or a documented exception applies.

AI contribution declaration

AI assistance is allowed; fully AI-generated or autonomous pull requests without meaningful human review are not accepted. See the AI contribution policy.

  • I have read and followed the AI contribution policy.
  • I have personally reviewed and understood all changes, tested them, and take responsibility for this contribution.

Component-specific

  • I have added a new component
  • I have added Unit Tests for my new component
  • I have modified an existing component
  • I have validated the Unit Tests for an existing component

… sort

- Add DefaultSortPriority to ColumnBase. Columns declaring
  IsDefaultSortColumn sort by priority (lower first); columns without
  one follow in declaration order, so grids without priorities behave
  as before
- Apply the declared sort once the columns are collected, so the first
  data request already carries it in priority order
- In single sort mode, the declared column that sorts first (lowest
  DefaultSortPriority) wins
- In single sort mode, RemoveSortByColumnAsync only keeps the column
  the grid is sorted by by default; removing the sort of any other
  column declaring IsDefaultSortColumn restores the default
- ResetSortAsync, Shift+S and removing the last sort level restore the
  sort in priority order
- Document DefaultSortPriority on the multi-column sorting page
…moved

In multiple sort mode, removing the last sort level (right-click on a
header, Clear in the header menu or RemoveSortByColumnAsync) brought
the declared default sort back, so sorts reappeared right after the
user removed them.

- Removing the last level now leaves the grid unsorted, as
  ClearSortAsync does, and saves it as unsorted in the URL.
  ResetSortAsync, Reset and Shift+S still return to the declared sort
- The header menu offers Reset whenever some column declares a sort and
  the grid is not sorted that way, including with one level or none,
  since Clear no longer goes back to the declared sort
- Single sort mode is unchanged
- Declare a second default sort column in the multi-sort demo and add
  a Reset sort button

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Shift+S cannot restore the declared sort after the final sort level is removed.

1 open finding
What changed in this PR

This follow-up to #5312 adds explicit default-sort priorities to DataGrid and separates clearing sorts from restoring defaults.

Changes:

  • Adds DefaultSortPriority for declared sort ordering.
  • Leaves the grid unsorted when its final sort level is removed.
  • Updates reset-menu visibility, regression tests, documentation, and demo controls.
File Description
tests/​Core/​Components/​DataGrid/​FluentDataGridMultiSortTests.razor Tests priority ordering, clearing, and reset behavior.
src/​Core/​Components/​DataGrid/​FluentDataGrid.razor.cs Implements ordered defaults and revised sort removal.
src/​Core/​Components/​DataGrid/​Columns/​ColumnSortMenuItems.razor Offers Reset when sorting differs from defaults.
src/​Core/​Components/​DataGrid/​Columns/​ColumnBase.razor.cs Adds the documented priority parameter.
examples/​Demo/​FluentUI.Demo.Client/​Documentation/​Components/​DataGrid/​Pages/​DataGridMultiSortPage.md Documents priorities and clear/reset semantics.
examples/​Demo/​FluentUI.Demo.Client/​Documentation/​Components/​DataGrid/​Examples/​DataGridMultiSort.razor Demonstrates multiple defaults and a reset button.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@dvoituron

Copy link
Copy Markdown
Collaborator

We've added a new requirement regarding PRs

@dvoituron

Copy link
Copy Markdown
Collaborator

Vincent Baaij (@vnbaaij) Have we received a request to add these new features?
I’m not sure we need DefaultSortPriority, since the existing SetSortAsync API already supports sorting independently of column order. Its main benefits are applying that order on the first request and restoring it through the built-in reset. But this adds features that may not be essential

@dvoituron Denis Voituron (dvoituron) added the status:needs-investigation Needs additional investigation label Oct 21, 2026
@vnbaaij

Copy link
Copy Markdown
Collaborator

No, we didn't get any requests for adding this.

In my opinion this is typically a feature that we need to have a discussion about before coding starts. The DataGrid is already a quity complex component as it is now. And I think it is OK if doesn't have all the bells and whistles.

@dvoituron

Copy link
Copy Markdown
Collaborator

No, we didn't get any requests for adding this.

In my opinion this is typically a feature that we need to have a discussion about before coding starts. The DataGrid is already a quity complex component as it is now. And I think it is OK if doesn't have all the bells and whistles.

I agree.

Steven Rasmussen (@StevenRasmussen) Could you start a discussion thread to tell us more about your motivation and get feedback from the community?

In the meantime, I'm closing this PR, which can be reactivated later if necessary.

@StevenRasmussen

Copy link
Copy Markdown
Contributor Author

Vincent Baaij (Vincent Baaij (@vnbaaij)) Have we received a request to add these new features? I’m not sure we need DefaultSortPriority, since the existing SetSortAsync API already supports sorting independently of column order. Its main benefits are applying that order on the first request and restoring it through the built-in reset. But this adds features that may not be essential

I was under the impression that by me taking the effort to build this feature, open the PR, and go through the review process, that I was "requesting" this feature. Also, I am a bit confused by this response. Why would you wait until the very end, when this PR has been reviewed multiple times, and is ready to be merged, to bring this up? Why waste my time and your time only to say "we should have a discussion about this first"? I would think that if you had any reservations about this PR that that would be the first comment. Anyway, I will do as you request and open a discussion to lay out the case/need for this feature.

@StevenRasmussen

Copy link
Copy Markdown
Contributor Author

Discussion here: #5497

@dvoituron

Copy link
Copy Markdown
Collaborator

I'm sorry. We really should have responded earlier.
We recently discussed this internally, and to avoid this kind of inconvenience, we're going to add a requirement that must be met when creating a PR: checking whether there is already an associated issue that has been discussed and approved by the community. This will prevent these wasted efforts.

In any case, if the request is approved, this PR will simply be reopened.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:needs-investigation Needs additional investigation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants