Checked against official sources on November 16, 2026
There is an array of numbers. A call to sort() should, one would think, put 8, then 9, then 10. The result puts 10 first.
The method did what its default rule says. With no compare function, sort() turns each item into text, then orders that text.
Everything below was run in Node.js v24.22.1.
The surprise in one line
console.log([10, 9, 8].sort()); It prints:
[ 10, 8, 9 ] The 10 is first. Another common case:
console.log([10, 1, 2].sort()); console.log([1, 10, 2, 20, 3].sort()); Those print [ 1, 10, 2 ] and [ 1, 10, 2, 20, 3 ].
Why the default treats them as text
The MDN page for Array.prototype.sort() says the default order is ascending, "built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values."
So 10 is compared as "10", and 8 is compared as "8". The first character of "10" is "1". The first character of "8" is "8". In that text order, "1" comes before "8", so 10 is placed before 8.
The same page gives the idea in numbers: in a numeric order, 9 comes before 80, but after the conversion to text, "80" comes before "9".
The fix: pass a compare function
To order numbers by size, give sort() a function that returns a number. MDN's memory aid is (a, b) => a - b for low to high.
console.log([10, 9, 8].sort((a, b) => a - b)); console.log([1, 10, 2, 20, 3].sort((a, b) => a - b)); Those print [ 8, 9, 10 ] and [ 1, 2, 3, 10, 20 ]. That is the numeric order most people want.
If the function returns a negative value, a comes first. If it returns a positive value, b comes first. Zero means they are treated as equal. The ECMAScript Language Specification defines this compare path in its section on Array.prototype.sort.
A second surprise: the original list changes
sort() edits the array it is called on. It then returns a reference to that same array. No new list is made.
const a = [3, 1, 2]; const b = a.sort(); console.log(a); console.log(b); console.log(a === b); It prints:
[ 1, 2, 3 ] [ 1, 2, 3 ] true a and b are the same object. A later edit through one name is an edit to the other.
To leave the original alone, use toSorted(). MDN says to use that method when you want a sort that does not change the first array. We ran it:
const d = [3, 1, 2]; const e = d.toSorted((x, y) => x - y); console.log(d); console.log(e); console.log(d === e); It prints:
[ 3, 1, 2 ] [ 1, 2, 3 ] false d is still [ 3, 1, 2 ]. e is a new array in numeric order.
Quick check you can use
- Are the items numbers? Do not rely on
sort()alone. Pass(a, b) => a - b, or another function that compares them as numbers. - Do you need the original order later? Use
toSorted(), notsort(). - Not sure what you got? Print the result of a small list you already know, such as
[10, 9, 8]. If10is first, the default text rule is in play.
Docs we opened
- MDN,
Array.prototype.sort() - MDN,
Array.prototype.toSorted() - ECMAScript Language Specification, Array.prototype.sort
About TalentOp Systems LLC
Our team works out of Omaha, Nebraska, and we have been in business since February 2025. We do IT staffing, software consulting and project development, and we staff software roles for clients in banking, insurance, healthcare and retail. Roles can be W2 or C2C, and many run 12+ months.
TalentOp Systems is a direct vendor for Kroger, whose offices are in Blue Ash, Ohio.
E-Verify: yes, TalentOp Systems LLC is enrolled and E-Verified.
Need H-1B sponsorship? TalentOp Systems LLC provides it for hires who meet the role's requirements and eligibility.
We also take H-1B transfers, and OPT or STEM OPT candidates are welcome to reach out.
Find us at www.talentopsystems.com or email info@talentopsystems.com.
