How to sort Numbers in JavaScript

In my latest Reddit Daily Programmer challenge, I came across the task of asking a user for numeric input and then sorting this input in numerical order. To sort numbers in JavaScript, we will use an array and the sort() function.

Table of Contents

ℹ️ This article has a corresponding Replit repository that you can copy and play around with. Create a free Replit account and click on the Fork Repl button below. This is the best way to practice programming!

Sort Numbers in JavaScript using an Array

Alright, let’s dive right into the code:

// Our Numbers Array let numbers = [22, 50, 1, 2, 39, 29, 10] // Our sort function function sortNumbers(array) { array.sort((a, b) => a - b) } // Call the function sortNumbers(numbers) // Log the result console.log(numbers) // How the function works function compareNumbers(a, b) { console.log(a - b) } compareNumbers(1, 5)
Code language: JavaScript (javascript)

This is as simple as it gets. JavaScript provides us with the sort() function, so we can utilize that. Let’s break the code down.

  1. We declare our array of numbers.
  2. We create a function that takes our array as an argument and then runs the sort function on the array.
  3. We are calling our sortNumbers(numbers) function with our numbers array as an argument.
  4. We console.log our now sorted numbers array.

The output looks as follows:

Sort numbers in JavaScript Output

How the sort() function works

The probably hardest part to understand in this code is how the actual sort() function works. Let’s break it down then, shall we?

The version I used in my code is simply a shorter version of the original function that was introduced in ES2015.

array.sort((a, b) => a - b)
Code language: JavaScript (javascript)

Let us look at the original version, which is maybe a bit easier to understand:

array.sort(function(a, b) { return a - b; });
Code language: JavaScript (javascript)

In essence, the function subtracts b from a, and if a positive value is returned, a is larger than b. If 0 is returned, both values are equal, and if something >0 is returned, a is less than b.

sort(1, 5) // It returns -4, meaning a is less than b.
Code language: JavaScript (javascript)

If you want to dig deeper into the ins and outs of this function, there is a great Stackoverflow answer to it. You can read it here.

Conclusion

It is fairly straightforward to sort integer values within an array to a numerical order. You probably will use the sort() function quite a bit, so you better get familiar with it.

If you want to learn how to find out if a certain value is present in an array, you might want to read up on the JavaScript includes() method!

🔥 Learn JavaScript Programming – Beginner Friendly!
🔨 JavaScript Basics
👉 JavaScript alert()
👉 Difference between Let and Const
🔸 JavaScript Arrays
👉 JavaScript Array Filter
👉 JavaScript Array Find
👉 JavaScript forEach (Arrays)
👉 JavaScript Slice (Arrays)
👉 JavaScript Spread Operator
🔸 JavaScript Strings
👉 JavaScript Slice(Strings)
👉 JavaScript Includes
🌐 Web Development with JavaScript
👉 Store User Input in a variable with JS
⚙️ JavaScript Specifics
👉 Sorting Numbers in JS
👉 JavaScript Fetch API
👉 toLocaleDateString in JavaScript

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap