JavaScript

JavaScript Array.sort() method | Explained

JavaScript offers multiple array methods that are used for various objectives, such as Array.includes(), Array.sort(), Array.forEach(), and so on. If we talk about the Array.sort() method, it is among one of the most commonly used array functions. It can be used to sort the alphabetic as well as numerical arrays. By default, it sorts the array elements in ascending order.

This article will present a detailed understanding of the below-listed concepts regarding the Array.sort() method:

So, let’s get started!

What is Array.sort() in JavaScript

It is a predefined method in JavaScript that sorts the array’s elements in ascending order. It sorts the string typed array elements ideally in alphabetically ascending order; however, it may produce faulty results while sorting numerical arrays.

Basic Syntax

The below-given code block will provide the basic syntax of JavaScript’s Array.sort() method:

Array.sort();

The Array.sort() method can take an optional parameter “compare_Function” to determine the sorting order:

Array.sort(compare_Function);

What does Array.sort() Method Return

In javaScript, the Array.sort() returns an array of sorted elements.

How Array.sort() Method Works in JavaScript

The Array.sort() method can take an optional parameter “compare_Function” which will return a zero, negative, or a positive value depending on the parameters. In such cases, the Array.sort() method will sort the array elements based on the returned value of the compare_Function:

function(x, y){return x-y}

If the compare_function returns a negative value then the sort() method will sort x before y.

Example: if x=50, y=100; while comparing the value of x with y, the sort method will call the compare_Function which will return a negative value i.e 50-100=-50; so, the sort method will sort x before y.

If the compare_function returns a positive value then the sort() method will sort y before x.

Example: If x=100, y=50; while comparing the value of x with y, the sort method will call the compare_Function which will return a positive value i.e 100-50= 50; so, the sort method will sort y before x.

If the compare_function returns a zero then the sort() method will keep the original order of x and y.

How to Use Array.sort() Method in JavaScript

Let’s consider some examples to understand how the Array.sort() method works in JavaScript.

Example1:

In this example, we will utilize the Array.sort() method to sort the array elements in ascending order:

<script>

var items = ["JavaScript", "C", "Python", "Java", "PHP"];

console.log("Sorted Array: " + items.sort());

</script>

The above code block will generate the following output:

The output verifies that the sort() method sorted the array in ascending order.

Example2:

Let’s consider the below snippet to understand how to sort an array in descending order:

<script>

var items = ["JavaScript", "C", "Python", "Java", "PHP"];

items.sort();

console.log("Sorted Array: " + items.reverse());

</script>

In this example, initially, we utilized the sort() method to sort the array elements, and after that, we utilized the reverse() method to reverse the order of sorted array elements:

In this way, we can sort the array elements in reverse order.

Example3:

Let’s consider the below snippet to understand how to sort a numeric array in ascending order using the sort() method:

<script>

var items = [10, 12, 5, 11, 33, 50];

items.sort(function (x, y) {

return x - y;

});

console.log("Sorted Array: " + items);

</script>

This time we passed compare function to the sort() method as a parameter, so the sort method will sort the array elements accordingly:

The output verifies that the sort method sorted the array elements in ascending order.

Example4:

This example will explain how to sort a numeric array in descending order using the sort() method:

<script>

var items = [10, 12, 5, 11, 33, 50];

items.sort(function (x, y) {

return y - x;

});

console.log("Sorted Array: " + items);

</script>

All the code remained the same as in the previous example except the return value of the compare function:

This is how the sort() method sorts the array elements in descending order.

Example4:

This example will explain how to find a greatest number from an array using the sort() method:

<script>

var items = [10, 12, 50, 11, 33, 5];

items.sort(function (x, y) {

return y - x;

});

var maximum =items[0];

console.log("Greatest Number: " + minimum);

</script>

The above code block will generate the following output:

The output authenticates the working of the sort() method.

Conclusion

Array.sort() is a predefined method in JavaScript that sorts the array’s elements in ascending order. It sorts the string typed array elements ideally in alphabetically ascending order; however, it may produce faulty results while sorting the numerical arrays. The compare function can be passed to the sort() method as an optional parameter to get the accurate results for the numeric arrays. This write-up explained what Array.sort() is? What it returns? and how to use it in JavaScript?

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.