In this article, we try to learn how can I sort an array in JavaScript, how to sort arrays from lowest to highest in JavaScript, how do you sort arrays, and how to sort arrays by name in JavaScript.
Sorting Arrays in JavaScript
Firstly, JavaScript is conveniently coming with a built-in method for sorting arrays and while the end result is the same, the various JavaScript engines implement this method using different sort algorithms like Quicksort or Insertion Sort, Merge sort, or Selection Sort.
So, this sort function is available as a prototype method on the array class like the below code:
Array.sort([compareFunc])
The compareFunc should have two parameters, a and b, and works like this below:
Firstly, if compareFunc returns 0 then the elements are treated as equal
Secondly, if compareFunc returns 1 then b is sorted before a
Thirdly, if compareFunc returns -1 then a is sorted before b
If you want then buy a good, reliable, secure web hosting service from here: click here
Hence, if the compareFunc is not given then the elements are converted to strings and then sorted alphabetically.
However, while sorting numbers seems like it should be straightforward, it can actually be a bit confusing like the below:
> let nums = [3, 2, 6, 50, 10]; > nums.sort() [ 10, 2, 3, 50, 6 ]
Previous JavaScript Articles
- JavaScript Variables
- JavaScript Operators
- Creating an Object in JavaScript
- Introduction to Asynchronous JavaScript
- Control Flow in JavaScript
- What is JavaScript Regex?
- JavaScript Events Example
- How to create a preloader in JavaScript?
- forEach method in JavaScript
As you can see, the numbers aren’t in the order you would have expected, and as a string, “50” comes before “6”, which is why 50 isn’t last in the array. To sort this array correctly, try the following instead code below:
> let nums = [3, 2, 6, 50, 10]; > nums.sort((a, b) => a - b); [ 2, 3, 6, 10, 50 ]
You can purchase your hosting from Cloudsurph.com, Cloudsurph hosting is a reliable hosting option for business and personal projects. We offer insight and help on system configuration issues and code errors or bugs.
So, if you want to reverse the order of the sorting, you need to just switch around the comparison of a and b. Hence, if we want the numbers to be in descending order, you can do the following code:
> let nums = [3, 2, 6, 50, 10]; > nums.sort((a, b) => b - a); [ 50, 10, 6, 3, 2 ]
Sorting Array of Objects
Basically, JavaScript using a comparator function like this makes it extremely easy to sort custom objects. Let’s say we have the following list of user data example:
let users = [ {name: 'Scott', age: '18'}, {name: 'Tom', age: '21'}, {name: 'Sally', age: '71'}, {name: 'Billy', age: '18'}, {name: 'Tim', age: '21'} ];
In this case, the order also highly depends on the application and we could provide a comparator that combines the two like the below:
users.sort((a, b) => { let keyA = a.age + a.name; let keyB = b.age + b.name; if (keyA < keyB) return -1; if (keyA > keyB) return 1; return 0; });
And as the result will be like a sorted array:
[ { name: 'Billy', age: '18' }, { name: 'Scott', age: '18' }, { name: 'Tim', age: '21' }, { name: 'Tom', age: '21' }, { name: 'Sally', age: '71' } ]
Finally, we saw how to use the built-in .sort() method to easily sort arrays and applies to any type of data, including strings, numbers, or even objects in JavaScript
So, the sort order is determined by the compareFun callback method, which is what allows you to determine the sort order or what object properties determine the sort order.
That’s it. If you enjoyed reading this article and have more questions please reach out to our support team via live chat or email and we would be glad to help you. we provide server hosting for all types of need and we can even get your server up and running with the service of your choice.