Dmitri Pavlutin 1/26/2021

array.sort() Does Not Simply Sort Numbers in JavaScript

Read Original

This article details a common JavaScript pitfall: the default array.sort() method converts numbers to strings and sorts them alphabetically, leading to unexpected results like [10, 11, 5]. It explains how to correctly sort numbers by providing a comparator function, such as (a, b) => a - b, and briefly mentions typed arrays as an alternative.

array.sort() Does Not Simply Sort Numbers in JavaScript

Comments

No comments yet

Be the first to share your thoughts!

Browser Extension

Get instant access to AllDevBlogs from your browser

Top of the Week