| by Arround The Web | No comments

Array sort() Method in JavaScript | Explained

Arrays are the fundamentals of a programming language as they allow us to use a set of elements of the same data types. It’s true that these arrays contain numerous amounts of data. Still, it is not sequential, which eventually causes an increase in loading time, and even it makes searching for elements in an array complex for the compiler. In order to avoid this problem, JavaScript provides a built-in array method.

In this write-up, we are going to discuss the array sort() method and focus on the following outcomes

What is the array sort() method in JavaScript?

This JavaScript method by default sorts an array in ascending order. We can also customize the sorting order by using customized functions as parameters. The most important thing is that while arranging the elements, this method focuses on the very first digit or a character of that number or a word that needs to be arranged. This method returns a new array as output after changing the original array.

Syntax:

array_name.sort()

In the above syntax, array_name represents the array variable. We can use the sort() method with or without arguments.

Code:

var pos=[30,150,42,81,20,21,35,23]
console.log(pos.sort())

In this code we take an array of unsequenced numbers and then we apply JavaScript built-in array sort() method on it to sort the array in a sequence. Here the sort() method compares the very first digit of all the numbers and arranges them accordingly.

Output:

As we can see in the above output, 150 is placed before every element as it is greater than every element in the array but here as sort() method focuses on the very first digit of a number so 1 < 2 that's why the compiler placed it at the start of the array.

How do we use the sort() method for numerical order?

We can also use the sort() method to arrange elements numerically in ascending or descending order. In order to arrange elements numerically, the sort() method compares the digits according to the given condition.

Code:

var pos=[30,150,42,81,20,21,35,23]
arrn = (m,n) => m-n
console.log(pos.sort(arrn))

In this code we create an array of un-arranged numbers. Then we create a function with the help of the arrow function to arrange elements in ascending order. Whereas the elements of the array are represented by m and n.

Output:

The output clearly shows that now each element is placed in numerically ascending order.

Note: To place elements in descending order just use n-m at the place of m-n.

How do we sort an array of string elements in JavaScript?

We can also arrange elements according to their length in an array by using the sort() method. In order to do that we need to find the length of elements first and then use the sort() method to sort the array.

Code:

var pos=["grapes","watermelon","fig","peach","plum"]
arrn = pos.map(x => x.length)
res = (m , n) => m-n  
console.log(arrn.sort(res))

In this code, we create an array of strings which comprises fruits. After that, we use the map method along with a function as a parameter to find the length of the array elements. Lastly, we create a function to place the elements at their right place and use it as a parameter in the sort() method which eventually arranges the elements in ascending order.

Output:

The output clearly shows that the array is sorted according to the length of elements in the array.

Conclusion

In JavaScript, the array sort() method by default arranges the array elements in ascending order. This method can also arrange elements in customized order with the help of user-defined functions. In this article, we have discussed JavaScript’s built-in array sort() method and arranged elements numerically as well as according to the element’s length.

Share Button

Source: linuxhint.com

Leave a Reply