Skip to content

Sorting direction not updating properly with manual sorting and same values on current page #5147

Closed

Description

@jenmak-tangelo

Describe the bug

When the data in the table column is all the same value (an empty string), the header.column.getIsSorted() function goes from ascending to false and never reaches descending.

Your minimal, reproducible example

Do not have

Steps to reproduce

Sort by a column with data that has empty values.
See that the empty values are shown first with ascending sorting.
Try to click again (execute header.column.getToggleSortingHandler())
And see that the sorting direction changes from ascending to false.

Expected behavior

The sorting direction should be changing to descending, instead of returning to false.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

macOS

react-table version

8.10.3

TypeScript version

5.1.6

Additional context

No response

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Activity

  1. changed the title [-]Sorting direction not updating properly with manual sorting and same values[/-] [+]Sorting direction not updating properly with manual sorting and same values on current page[/+] on Oct 30, 2023
  2. zsilbi commented on Nov 13, 2023

    @zsilbi

    I have the same problem.

    1. I call column.toggleSorting()
    2. column.getAutoSortDir() returns asc because the first value is a string, so the column will be sorted asc initially.

    column.getAutoSortDir = () => {
    const firstRow = table.getFilteredRowModel().flatRows[0]
    const value = firstRow?.getValue(column.id)
    if (typeof value === 'string') {
    return 'asc'
    }
    return 'desc'
    }

    1. I fetch the sorted data to the table from the API and the first value of the column is now null instead of a string.
    2. I call column.toggleSorting() again
    3. column.getAutoSortDir() now returns desc instead of asc because the first value is null
    4. That's why column.getNextSortingOrder() decides to turn off sorting for the column by returning false because it thinks it had already been ordered desc previously

    My current workaround is to set the sortDescFirst: true manually for the column (or globally for the table), so the getAutoSortDir() won't be used.

    /**
    * Set to `true` for sorting toggles on this column to start in the descending direction.
    * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/sorting#sortdescfirst)
    * @link [Guide](https://tanstack.com/table/v8/docs/guide/sorting)
    */
    sortDescFirst?: boolean

  3. tomdubraw commented on Dec 11, 2023

    @tomdubraw

    Same issue, thanks @zsilbi, we use your trick as a temp workaround.

    Any updates to have a proper way to have ASC first without breaking the lib ?

  4. Oberin98 commented on Jan 15, 2024

    @Oberin98

    I also encountered this problem when implementing manual sorting. After some time of debugging I found the problem. This is caused by comparing the value of a column with a string, which leads to a change in the default auto sort direction value, when value was a string during the first sort and null or undefined during the second.

    Screenshot 2024-01-15 at 12 57 43

    Maybe you should define a default auto sort direction based only on sortDescFirst?

  5. Oberin98 commented on Jan 15, 2024

    @Oberin98

    May be this example will be helpful to someone
    Screenshot 2024-01-15 at 16 56 48

  6. eliranma commented on Mar 17, 2024

    @eliranma

    @Oberin98
    I tried use your snippet above, but it doesn't remove my sorting, the "asc->desc" flow goes right but i can remove the sorting.

    const handleSorting = () => { const isSortable = column.getCanSort(); if (isSortable) { const ctx = header.getContext(); const isSorted = header.column.getIsSorted(); const isSortDescFirst = !!header.column.columnDef.sortDescFirst || !!ctx.table.options.sortDescFirst; const isEnableSortingRemoval = !!ctx.table.options.enableSortingRemoval; if (!isSorted) { header.column.toggleSorting(isSortDescFirst); } if (isSorted === "asc") { if (isSortDescFirst && isEnableSortingRemoval) { header.column.clearSorting(); } else { header.column.toggleSorting(true); } } if (isSorted === "desc") { if (!isSortDescFirst && isEnableSortingRemoval) { header.column.clearSorting(); } else { header.column.toggleSorting(false); } } } };

    And my usage:

    <div {...{ className: header.column.getCanSort() ? "cursor-pointer select-none " : "bg-slate-50", onClick: handleSorting, }} > {flexRender(header.column.columnDef.header, header.getContext())} {{ asc: "🔼", desc: "🔽", false: "", }[header.column.getIsSorted()] ?? null} </div>

  7. Oberin98 commented on Mar 18, 2024

    @Oberin98

    @eliranma did you set enableSortingRemoval option to true?

  8. eliranma commented on Mar 20, 2024

    @eliranma

    @Oberin98
    Yap,
    // Sorting
    enableSorting: true,
    onSortingChange: handleSorting,
    enableSortingRemoval: true,
    enableMultiRemove: true,
    sortDescFirst: true,
    manualSorting: true,

  9. eliranma commented on Mar 26, 2024

    @eliranma

    Hi Guys, any suggestion? maybe downgrade to previous version?

  10. arkdev9 commented on Mar 8, 2025

    @arkdev9

    Is there any plan to fix this? The workaround I am using is

    ...
    manualSorting: true,
    sortDescFirst: true,
    ...
    
  11. sneakylenny commented on Mar 13, 2025

    @sneakylenny

    @eliranma @arkdev9

    We decided to overwrite the function responsible for this as we prefer it to be always "asc" in any case:

    header: ({ column }) => {
      column.getAutoSortDir = () => 'asc'
    }

    But you could write your own logic.

  12. added a commit that references this issue on Aug 3, 2026
    c35d70d
  13. added a commit that references this issue on Aug 3, 2026
    aad2c29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions