Skip to content

fix[sortBy, orderBy]: sort symbol values instead of throwing TypeError - #2228

Open
Ultroncn wants to merge 2 commits into
toss:mainfrom
Ultroncn:fix/sortBy-symbol-values
Open

Ultroncn wants to merge 2 commits into
toss:mainfrom
Ultroncn:fix/sortBy-symbol-values

Conversation

@Ultroncn

Copy link
Copy Markdown

sortBy / orderBy throw TypeError: Cannot convert a Symbol value to a number when a sort key is a Symbol, because the shared compareValues compares regular values with < directly. lodash and the compat build sort Symbol values fine, so the main package crashing here is a bug.

This ranks Symbols alongside the other special values in nullishRank (after regular values, before NaN/null/undefined, matching the main package's existing NaN < null < undefined order), so Symbols never reach the < comparison. Two Symbols compare equal (rank tie returns 0), keeping the sort stable.

Verified locally: the 3 new tests fail on the old implementation with the TypeError and pass with the fix; src/array/sortBy.spec.ts + src/array/orderBy.spec.ts 20/20 pass, compat sortBy/orderBy specs 43/43 pass.

Note: this follows the main package's own ordering convention rather than lodash's (lodash puts NaN last); only the compat build promises lodash parity, and the main package's existing tests pin NaN < null < undefined.

Sorting by a criterion that returns a symbol threw
'TypeError: Cannot convert a Symbol value to a number',
because compareValues compared regular values with '<'.
Rank symbols after regular values (before NaN, null and
undefined, keeping the existing order), so they sort
stably like lodash and es-toolkit/compat do.

Signed-off-by: Ultroncn <82553854+Ultroncn@users.noreply.github.com>
@vercel

vercel Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
es-toolkit Ready Ready Preview Nov 19, 2026 2:26pm UTC

Request Review

This branch was successfully deployed

1 active deployment
Preview — b27375ec Deployed Nov 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant