Day 11 of 30 of JavaScript
Tags: webdev, javascript, beginners, tutorial
Published: 2024-05-20 10:29:31

Hey there👋. Hope you are doing well😊.
In the last post we have read about some array methods used in JavaScript, in this post taking our discussion further I am going to tell you about some more array methods that are useful.
So let's get started...

## Sorting an Array

Sorting an array refers to arrangement of array elements in particular order (ascending, descending or random).

**`sort()` Method**

The `sort()` method sorts the array lexicographically in dictionary order).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g5a60dne0aoq0spde6s.png)
If we want array to be sorted in descending order then we can simply sort the array and then reverse it.

- Note that this method make changes in original array to avoid this we can use a more safer method **`toSorted()`**. It does not make change in original array rather it returns a new array.

- `sort()` uses a variant of a Timesort algorithm which is a hybrid algorithm derived from merge and insertion sort. Timsort is known for its efficiency and is designed to perform well on many kinds of real-world data. 

- The `sort()` method works well for strings but it will fail for numerical values. You can see the case below-:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6jzksxi5djeq3fw9r2ss.png)
So you can see that in `sort()` method sort values in dictionary order that is why 9 is larger than 67.
To solve this problem we make slight modification in `sort()` to make it work.

## Numerical Sort

We can fix the problem stated above by providing a compare function in `sort()` method.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7segymlbvfyemp2yqmd.png)
Here's how the sort method works step-by-step with the provided comparison function:

1. Comparison Function: The function `function(a, b) { return a - b; }` is used to compare two elements, a and b. This function returns:
- A negative number if a is less than b (which means a should come before b).
- Zero if a and b are equal.
- A positive number if a is greater than b (which means a should come after b).

2. Sorting Process: The sort method will repeatedly call the comparison function with pairs of elements from the array to determine their order. It uses an efficient sorting algorithm to sort the elements based on the results of these comparisons.

The sort method with the comparison function `function(a, b) { return a - b; }` ensures that the array is sorted numerically in ascending order.

We can change the comparison function according to our need and sort the array accordingly.

> The sort method expects the comparison function to return a negative number, zero, or a positive number to determine the order of elements.

Apart from using `sort()` method and comparison function we can also use different techniques to sort our array. 
One such method is using external library called Lodash, which provide convenient methods for sorting arrays.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2d2nhj770qrbv4ktwt7.png)

## Finding maximum and minimum element in array

**`Math.min.apply()` method**

We can use `Math.min.apply` to find the lowest number in an array.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9qnpdsumypl7f9i2c4r.png)
The `Math.min` function is a built-in JavaScript function that returns the smallest of zero or more numbers. However, it does not accept an array directly as an argument.
`Math.min.apply(null, arr)` is used to call `Math.min` with the elements of the array `arr` as individual arguments.

**`Math.max.apply()` method**

We can use `Math.max.apply` to find the lowest number in an array.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c49eeqhc980802yf0cey.png)

This is it for this blog. I hope you have understood it. In the next blog we are going to see Array iteration.
Till then stay connected and don't forget to follow me🤍