Sort numbers from an array in javascript 1

Sort numbers from an array in javascript

const bignumbers = [66, 58, 81, 444, 92, 9, 6, 13, 2];
const sortedNumbers = bignumbers.sort(function (a, b) {
    return a - b;
})
console.log(sortedNumbers);
//Output: [2, 6, 9, 13, 58,66, 81, 92, 444]

Here is what the above code is Doing:
1. We have an array of numbers.
2. We use the sort() method to sort the numbers in ascending order.
3. We pass a function to the sort() method.
4. The function takes two parameters, a and b.
5. The function returns a – b.
6. The sort() method sorts the numbers in ascending order.

Similar Posts