# Implementation of Arrays sort method in Javascript

# What is an Array

An array is an ordered collection of similar types of elements. It is used when we want to store a list of elements and access them in a single variable. In most languages, the arrays are a reference to multiple variables but in javascript, an array is a single variable that stores multiple elements. Javascript has many different types of methods in an array which helps us to access and process the data.

In this article, we can talk about the Array. sort() method, and let’s see how this function works with a different scenario.

# Array.sort() method in Javascript

Sort is a term that describes the process of organizing data in a particular manner to allow for information to be found easier. For example, In our phone contacts, the names and contact information are sorted in alphabetical order so that we have easy to see the name if it’s available.

In Array. sort() method, it sorts elements present in an array and returns the sorted array to the user. The sort() method converts each element present in an array into a string and compares them with UTF-16 order.

**Syntax**: arr.sort([compareFunction])

By default, the sort() method is sorted the string elements of an array in ascending order. See the example below.

let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];

let sortedName = names.sort();console.log(sortedName);// output: ['Alice', 'Alice', 'Bob', 'Bruce', 'Tiff'];

Here we can simply call the method and it will sort the array. The sort order is determined by converting each item to a string and sorting them alphabetically in ascending order.

Let see some more example using this method. We can add two more names in an array in lowercase and see what output comes.

let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice', 'sam', 'adam'];let sortedName = names.sort();console.log(sortedName);// output: ['Alice', 'Alice', 'Bob', 'Bruce', 'Tiff', 'adam', 'sam']

Here we can use the same method but the output is not sorted the way we want. But why? What is the main problem behind the outcomes?

In the above syntax, compare function is defined inside the sorted function. Let us know what is the use of compare function in the sort() method.

The compare function sorts the elements present in an array based on different parameters in different orders. Let takes two parameters ‘parameter A’ and *‘parameter B’ *and see how compare function works.

* If

‘parameter A’is less than ‘parameter B’ in some orders then we can return -1, which means ‘parameter A’ will be sorted before than ‘parameter B’.

* If ‘parameter A’ is greater than ‘parameter B’ in some orders then we can return 1 which means ‘parameter B’ will be sorted before than ‘parameter A’.

* If ‘parameter A’ is equal to ‘parameter B’ then it returns 0 which ‘parameter A’ and parameter B is unchangeable.

So, implement the compare function in the second example and see whats the outputs are coming from.

let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice', 'sam', 'adam'];let sortedName = names.sort((a,b) => {

let a1 = a.toLowerCase();

let b1 = b.toLowerCase();

if (a1 < b1) {

return -1;

}

if (a1 > b1) {

return 1;

}

return 0;

});console.log(sortedName);// output: ['adam', 'Alice', 'Alice', 'Bob', 'Bruce', 'sam', 'Tiff']

In the above example, the sort method implemented a compare function with two parameters *‘a’ *and *‘b’*.* *We have to convert the all-string characters either in lowercase or in uppercase. So that it will compare in an equal manner. Then names are compared according to a given condition and give the perfect output.

We can see the outputs in ascending order. What if we see our outputs in descending order. Let’s take the above example and see what changes are done to get our outputs in descending order.

let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice', 'sam', 'adam'];let sortedName = names.sort((a,b) => {

let a1 = a.toLowerCase();

let b1 = b.toLowerCase();

if (a1 < b1) {

return 1;

}

if (a1 > b1) {

return -1;

}

return 0;

});console.log(sortedName);// output: ['Tiff', 'sam', 'Bruce', 'Bob', 'Alice', 'Alice', 'adam']

So, nothing is changed in code except the return values in a specific condition.

If *a1 is less than b1* then it *returns 1* and if *a1 is greater than b1* then it* returns -1*.

We can see the examples in an array of strings elements but the same thing is work on integer/number elements. Yes, but in a different process. Let see how the array can sort the number of elements.

//Example - 1

let num1 = [4,1,3,2]

console.log(num1.sort()) //[1,2,3,4]//Example-2

let num2 = [6,600,1,200,40]

console.log(num2.sort()) //[1,200,40,6,600]

In the above code, one-digit elements in the num1 array return a sorted array. But more than the one-digit element in the num2 array not returns a sorted array. So, when we sorted elements like more than one digit number or decimal numbers then we use compare function inside the sorted function. Let see example-2 with using of compare function:

`//Example-2`

let num2 = [6,600,1,200,40]

console.log(num2.sort((a,b) => a-b) //[1, 6, 40, 200, 600]

In the above code, we can use two parameters in compare function and subtract the two parameters. It returns negative value, 0, positive value and works like this:

* If the result is negative, then a is sorted before b

* If the result is 0, then no change appears in value a and b

* If the result is positive, then b is sorted before a

Similarly, if we want to sort the number elements in descending order do like this:

`//Example-2`

let num2 = [6,600,1,200,40]

console.log(num2.sort((a,b) => b-a) //[600, 200, 40, 6, 1]

Here we can see the use of the sort method in different scenarios. It is important to understand the background concepts of all methods not only Javascript, in every programming language so that you can make better decisions to implement in your code.