← back to flaviocopes.com

Sort Comparator Builder

← All tools

Add sort keys, preview on sample or pasted JSON, copy a readable comparator plus a one-line toSorted() usage.

~~~

Sort keys

Preview data

Generated code

Gotchas: Default sort() compares strings — [1, 10, 2] stays lexicographic.

Subtraction (a - b) breaks on NaN and coerces strings.

sort() mutates the array; use toSorted(compare) for a new copy.

~~~

About this tool

Multi-key comparators get verbose fast. This builder handles nulls, localeCompare, and nested properties — then shows the sorted output live.

~~~

Read more