Default sort() compares values as strings. This demo uses numeric compare functions so values such as 4 and 32 are ordered numerically.
const numbers = [2, 22, 4, 24, 32];
const ascending = [...numbers].sort((a, b) => a - b);
const descending = [...numbers].sort((a, b) => b - a);
console.log(ascending);
console.log(descending);
Array sort() Tutorial Alphabetical Demo Display Array Elements
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.