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

JavaScript sort() compares items as strings unless you pass a comparator. That is why [1, 10, 2] stays wrong. Real tables need multi-key sorting — last name, then first name, then age — plus null handling and locale-aware strings.

This builder turns your rules into a compare function. Paste JSON to preview the sorted output, then copy the comparator and atoSorted() one-liner so you do not mutate the original array. Nested properties like user.age work in the property field.

~~~

Read more