How to Sort Numbers in Ascending Order in JavaScript

Sort Numbers in Ascending Order in JavaScript

Sorting numbers in JavaScript might seem easy at first, but there’s a twist. Unlike some other languages, JavaScript’s built-in sort() method sorts elements as strings by default. This can lead to surprising results if you’re not careful. For example, [10, 2, 5] would be sorted as [10, 2, 5]—not what you'd expect!

Thankfully, with a proper compare function and a few tricks, you can easily sort numbers the way you want. In this tutorial, we’ll walk through beginner-friendly ways to sort numbers in ascending order using the sort() method and manual approaches. Each example is paired with actual output and explained step-by-step.

Method 1: Using sort() with a Compare Function

The most common and correct way to sort numbers in ascending order is to provide a compare function to sort(). This function determines the order by subtracting two elements. If the result is negative, a comes before b.

const numbers = [42, 7, 13, 2, 100];

numbers.sort((a, b) => a - b);

console.log("Sorted numbers:", numbers);
Sorted numbers: [ 2, 7, 13, 42, 100 ]

Method 2: Using sort() Without a Compare Function – What NOT to Do

Let’s take a moment to highlight what goes wrong when you forget the compare function. JavaScript treats numbers as strings, leading to strange alphabetical ordering. This is a common mistake among beginners.

const nums = [42, 7, 13, 2, 100];

nums.sort();

console.log("Incorrect sort:", nums);
Incorrect sort: [ 100, 13, 2, 42, 7 ]

Method 3: Manual Sorting with a Bubble Sort (for learning purposes)

Although not used in real-world apps due to inefficiency, implementing a manual sort helps you understand how sorting works under the hood. Here's a basic bubble sort implementation that arranges numbers in ascending order.

const nums = [5, 3, 8, 4, 1];

for (let i = 0; i < nums.length; i++) {
  for (let j = 0; j < nums.length - 1; j++) {
    if (nums[j] > nums[j + 1]) {
      [nums[j], nums[j + 1]] = [nums[j + 1], nums[j]];
    }
  }
}

console.log("Manually sorted:", nums);
Manually sorted: [ 1, 3, 4, 5, 8 ]

When Should You Use Each Method?

Use the sort() method with a compare function for real-world numeric sorting—it's clean, fast, and expressive. Avoid sorting numbers without a compare function unless you're working with strings. Use manual sorting like bubble sort only as a learning exercise or when building your own custom logic from scratch.

Final Thoughts

Sorting numbers in ascending order is foundational to clean, reliable data handling in JavaScript. Whether you’re working with scores, dates, or numerical values from an API, knowing how to sort properly ensures your application behaves as expected.

Remember: sort() is powerful, but it needs guidance for numbers. By mastering these sorting techniques, you're adding one more sharp tool to your JavaScript toolkit. Play with it, test edge cases, and keep practicing—you’ll be sorting like a pro in no time.

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...