Skip to content

[Bug] Table sort: a single NaN cell silently corrupts the order of valid numbers (comparator returns NaN) #3585

Description

@arham766

Description

defaultCompare in packages/core/src/Table/plugins/sortable/useTableSortableState.tsx has a number fast path:

if (typeof aVal === 'number' && typeof bVal === 'number') {
  return aVal - bVal;
}

typeof NaN === 'number', so a NaN cell takes this path and the comparator returns NaN, which Array.prototype.sort treats as +0 ("equal"). That makes the comparator inconsistent (NaN compares equal to both 5 and 1 while 5 > 1), and the sort skips insertion positions for other, valid rows.

Reproduction (executed)

useTableSortableState({
  data: [{id:'a', score:5}, {id:'b', score:NaN}, {id:'c', score:1}, {id:'d', score:3}],
  defaultSort: [{sortKey: 'score', direction: 'ascending'}],
});
// sortedData scores: [5, NaN, 1, 3]   ← 5 renders before 1 and 3

Expected: valid numbers in ascending order with NaN grouped at an end (like null/undefined already are). The corruption is data-dependent — some datasets survive, others don't — which makes it look intermittent.

Why it matters

NaN is a routine value (failed parseFloat, 0/0, missing metric). The user clicks "sort by score" and gets a column of wrongly ordered valid numbers under correct-looking sort chrome — silent, and worse than a crash.

Suggested fix

Route NaN through the same "sort to the end" branch as null/undefined before the fast path. Happy to send the PR.

Astryx Version

main (d9932ea)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions