DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 11 of 30 of JavaScript

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
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
    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
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:
  2. A negative number if a is less than b (which means a should come before b).
  3. Zero if a and b are equal.
  4. A positive number if a is greater than b (which means a should come after b).

  5. 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

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
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

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🤍

Top comments (0)