Skip to content
Source
Sign inSign up
TanStack/table

fix(table-core): flatten sorted parent rows ahead of their sub-rows #6529

MergedJaybhade(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/6529/headmerged by KevinVandy

🎯 Changes

_createSortedRowModel recurses into a row's sub-rows before pushing the row itself, so getSortedRowModel().flatRows comes out in post-order — every descendant precedes its own parent.

With two parent rows that each have sub-rows, sorted ascending:

table.getCoreRowModel().flatRows.map((r) => r.id)
// ['a', 'a1', 'a2', 'b', 'b1']

table.getSortedRowModel().flatRows.map((r) => r.id)
// before: ['b1', 'b', 'a2', 'a1', 'a']
// after:  ['b', 'b1', 'a', 'a2', 'a1']

That disagrees with the model's own rows, which are already in render order, and with how the rest of the pipeline flattens:

  • createCoreRowModel pushes a row, then recurses into its sub-rows.
  • createPaginatedRowModel's handleRow does the same.
  • addSubRowsToFlatArrays (added for the maxLeafRowFilterDepth case in #6395) is explicitly parent-first, to keep "the flat representation ... consistent with the visible tree".

The fix takes the row's slot before descending, and overwrites that slot when the branch is cloned, so flatRows still holds the same instance as rows — which the previous sortedFlatRows.push(cloned) guaranteed and a plain push before recursion would have lost.

Deliberately not included

filterRowModelFromLeafs and filterRowModelFromRoot flatten the same way, so getFilteredRowModel().flatRows is post-order too. I left those alone: column_getAutoSortFn and column_getAutoFilterFn sample getFilteredRowModel().flatRows.slice(0, 10), so reordering that array changes which rows drive automatic fn detection (cf. #4908). That felt like a bigger behavioral call than this fix — happy to do it in a follow-up if you'd like it changed.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

pnpm run test:pr is green — test:eslint, test:sherif, test:knip, test:lib, test:types, test:build, build across 408 projects, with @tanstack/table-core:test:lib at 62/62 files and 1288/1288 tests.

Two tests added to createSortedRowModel.test.ts, both verified to fail on main and pass with the fix:

  • flattens each parent ahead of its own sub-rows — the ordering itself.
  • flattens a branch clone rather than the row it replaced — pins flatRows[0] === rows[0] so the clone, not the row it replaced, is what lands in flatRows.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes

    • Corrected sorted table output so parent rows consistently appear before their sub-rows in flattened results.
    • Ensured flattened results reference the correctly sorted row branches.
    • Improved consistency for grouped and worker-backed sorted table models.
  • Tests

    • Added coverage for parent and descendant ordering in sorted flattened rows.
    • Expanded coverage for grouped models and depth handling.
    1. Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 9c76e16a-2941-4c22-b7ec-bcc8b5910eb1

      📥 Commits

      Reviewing files that changed from the base of the PR and between 794da817f531678ae8958e02b42da8ad661cd76e and 726f21bed94cbdee569b2b448852249aec446f95.

      📒 Files selected for processing (6)
      • .changeset/great-pugs-sniff.md
      • packages/table-core/src/worker/createWorkerRowModel.ts
      • packages/table-core/src/worker/rebuildRowModel.ts
      • packages/table-core/tests/implementation/features/row-sorting/createSortedRowModel.test.ts
      • packages/table-core/tests/unit/worker/serializeRebuild.test.ts
      • perf-done.md
      🚧 Files skipped from review as they are similar to previous changes (1)
      • .changeset/great-pugs-sniff.md

      📝 Walkthrough

      Walkthrough

      The sorted row model now keeps parent rows before descendants in flatRows. Worker rebuilding uses explicit stages to preserve this ordering and depth behavior. Tests cover nested sorting, grouped worker round trips, and row identity.

      Changes

      Sorted flat-row ordering

      Layer / File(s)Summary
      Preserve parent positions
      packages/table-core/src/features/row-sorting/createSortedRowModel.ts
      sortData reserves each parent position before sorting descendants and replaces it with a cloned branch row when needed.
      Rebuild sorted worker rows
      packages/table-core/src/worker/createWorkerRowModel.ts, packages/table-core/src/worker/rebuildRowModel.ts
      Worker rebuilding receives a TableWorkerStage. Sorted stages reserve synthetic group-row positions, while depth resets remain stage-specific.
      Validate ordering and identity
      packages/table-core/tests/implementation/features/row-sorting/createSortedRowModel.test.ts, packages/table-core/tests/unit/worker/serializeRebuild.test.ts, .changeset/great-pugs-sniff.md, perf-done.md
      Tests verify parent-first flattening, nested sorted row identity, worker round trips, and depth behavior. Release and performance notes document the correction.

      Estimated code review effort: 3 (Moderate) | ~25 minutes

      Suggested reviewers: kevinvandy

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely identifies the fix to place sorted parent rows before their sub-rows.
      Description check✅ PassedThe description explains the change, motivation, tests, checklist completion, release impact, and changeset.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. View your CI Pipeline Execution ↗ for commit 726f21bed94cbdee569b2b448852249aec446f95

      CommandStatusDurationResult
      nx affected --targets=test:eslint,test:sherif,t...✅ Succeeded7m 42sView ↗
      nx run-many --targets=build --exclude=examples/**✅ Succeeded1m 1sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-08-08 17:47:54 UTC

    3. More templates

      @tanstack/alpine-table

      npm i https://pkg.pr.new/@tanstack/alpine-table@6529

      @tanstack/angular-table

      npm i https://pkg.pr.new/@tanstack/angular-table@6529

      @tanstack/angular-table-devtools

      npm i https://pkg.pr.new/@tanstack/angular-table-devtools@6529

      @tanstack/ember-table

      npm i https://pkg.pr.new/@tanstack/ember-table@6529

      @tanstack/lit-table

      npm i https://pkg.pr.new/@tanstack/lit-table@6529

      @tanstack/match-sorter-utils

      npm i https://pkg.pr.new/@tanstack/match-sorter-utils@6529

      @tanstack/octane-table

      npm i https://pkg.pr.new/@tanstack/octane-table@6529

      @tanstack/preact-table

      npm i https://pkg.pr.new/@tanstack/preact-table@6529

      @tanstack/preact-table-devtools

      npm i https://pkg.pr.new/@tanstack/preact-table-devtools@6529

      @tanstack/react-table

      npm i https://pkg.pr.new/@tanstack/react-table@6529

      @tanstack/react-table-devtools

      npm i https://pkg.pr.new/@tanstack/react-table-devtools@6529

      @tanstack/solid-table

      npm i https://pkg.pr.new/@tanstack/solid-table@6529

      @tanstack/solid-table-devtools

      npm i https://pkg.pr.new/@tanstack/solid-table-devtools@6529

      @tanstack/svelte-table

      npm i https://pkg.pr.new/@tanstack/svelte-table@6529

      @tanstack/table-core

      npm i https://pkg.pr.new/@tanstack/table-core@6529

      @tanstack/table-devtools

      npm i https://pkg.pr.new/@tanstack/table-devtools@6529

      @tanstack/vue-table

      npm i https://pkg.pr.new/@tanstack/vue-table@6529

      @tanstack/vue-table-devtools

      npm i https://pkg.pr.new/@tanstack/vue-table-devtools@6529

      commit: 726f21b

    4. KevinVandy merged this269e0d8

    5. KevinVandy closed this

Sign in to join this conversation.