JavaScript

Javascript Sort

Javascript Sort
As we have to manage arrays in almost all programming languages, JavaScript is no different. Arrays are usually used to store data like strings, numbers, objects, and undefined. With the exponential growth of online data, we frequently need to manage and sort the data. Sorting is kind of a massive experience in almost every programming language. It takes a lot of effort, machine power, and calculations to do the right sorting. With the expansion of data, we need to sort and structure the data in a beautiful way. Javascript provides a built-in array mutator method sort() for sorting arrays. In this article, we will have a look at Javascript's built-in sort() method and learn what the Javascript sort method is, as well as how we can use it for our purpose to sort elements in an array. Let's go ahead and start working!

The sort method is used to arrange different elements in an array in a specific order.

Syntax

The general syntax for the sort method is:

array.sort();

This method returns the sorted array in ascending order by default.

We would discuss a couple of examples to understand the sort method in JavaScript.

Examples

We suppose an array of string in which we have some different names of Linux operating systems.

let arr = ["Ubuntu", "Fedora", "CentOS", "Debian", "Kali Linux"]

Now, if we apply the sort method over this array:

arr.sort();

It will definitely sort the array in alphabetical order. We can see the output in the screenshot below.

But, if we want to get the string in reverse/descending order. We can apply a Javascript's built-in reverse function over the sorted array like this:

var sortedArray = arr.sort();
sortedArray.reverse();

The shorter way to do the reverse is:

arr.sort().reverse();

Alright! It worked fine for the string. Let's try if it works for the numbers as well.
So, we first suppose an array of numbers.

let arr = [14,8,33,27,6]

Then apply the sort method over the array of numbers.

arr.sort();

It seems like it didn't work well as it did for the string. Because the sort method first converts the numbers into the strings and then sorts on the base of Unicode. Although, “8” comes before “14” in numerical order. But, in UTF-16 code units order, “14” comes before “8”. The good thing in Javascript, we got the solution for this.

CompareFunction

Here comes the concept of compare function that comes in handy in helping sort the numbers. We can use a compare function to the sort method as a callback function, which takes two elements. It then sorts them according to our requirement in the compare function and returns them to the sort method, continuously doing this until it reaches the end of the array.

The syntax for the sort method with the compareFunction would be like this:

array.sort(compareFunction);

Now, if we take a look at the technical details of the compareFunction, that is how it actually works. If we do not provide a compareFunction to the sort method, it will sort according to the UTF-16 code unit orders. If we utilize the compareFunction, all elements would be sorted in accordance with the return value of compareFunction. So, if we want to write a compare function for the numbers. That would be just like this:

function (a, b) return a - b

CompareFunction takes two values at a time and returns three types of values.
True or “1”, if the first value comes before the second value or the first value is greater than the second value:
False or “-1”, if the first value comes after the second value or the first value is greater than the second value.
And “0”, if two values are equal.

Now, if we try to apply it to sort the array of numbers. We can apply it like this:

arr.sort(function (a ,b) return a - b )

As you can see in the output, array having numbers have been sorted decently.

The shorter way of doing the same task will be like this:

arr.sort((a, b) => a - b)

But, this works only for the comparison of the numbers.

We can also use the sort method to sort the array of objects depending on the values of the object, which we want to sort the array of objects. If suppose we would want to sort on the base of the number of users an array of objects in which every object includes the Linux Operating Systems and the number of their users, then we will be using the following:

arr = [
name:"Ubuntu", users:3000
name:"Fedora", users:1500
name:"CentOS", users:2000
name:"Debian", users:5000
name:"Kali Linux", users:4000
]

So, in order to sort on the base of users. The sort function would be like this:

arr.sort(() => return a.users - b.users )

So, these are the different ways of using the sort method to sort the arrays of any type.

Conclusion

In this article, we have learned how we can sort an array of different types using Javascript's built-in sort function. This article explains the concept of the sort function from novice to intermediate level in a very easy, profound, and effective way. So, keep on learning, working, and getting experience in Javascript with linuxhint.com to have a better grasp over it. Thank you so much.

WinMouse lets you customize & improve mouse pointer movement on Windows PC
If you want to improve the default functions of your mouse pointer use freeware WinMouse. It adds more features to help you get the most out of your h...
Mouse left-click button not working on Windows 10
If you are using a dedicated mouse with your laptop, or desktop computer but the mouse left-click button is not working on Windows 10/8/7 for some rea...
Cursor jumps or moves randomly while typing in Windows 10
If you find that your mouse cursor jumps or moves on its own, automatically, randomly while typing in Windows laptop or computer, then some of these s...