JavaScript’s sort() function call accepts a custom comparison function as a parameter for sorting an array of arrays. JavaScript’s sort() function call accepts a custom comparison function as a parameter for sort an array of arrays.

Array class is a class containing static methods that are used with arrays in order to search, sort, compare, insert elements, or return a string representation of an array, in an array.

There are two versions of the Arrays.sort() method. If we need to sort a specific section using this method of the Arrays class, we overload it and pass the starting and last index to the array. There are two ways to sort down an array, whether it is an integer array or a character array, one in which we do not pass any arguments.

				
					const data = [["This", 3], ["just", 6], ["there", 12], ["is", 2]]
const sortedData = data.sort((a, b) => b[1] - a[1])
console.log(sortedData)
				
			

Result:

				
					[['there', 12], ['just', 6], ['This', 3], ['is', 2]]
				
			

How Does It Work?

You must comprehend how fundamental sorting in JavaScript functions in order to learn how this works.

JavaScript Sorts Alphabetically by Default

In JavaScript, sorting is done alphabetically by default. Not at all what we wanted.
You must define a sorting function that is applied to subsequent items of the array until the array is completely sorted in order to sort numerically.

Example 1. Sort an Array of Numbers in Descending Order

For example, let’s sort a list of numbers in increasing order.

				
					var numArray = [3, 1, 2];

numArray.sort(function(a, b) {
  return a - b;
});

console.log(numArray);
				
			

Output:

				
					[1, 2, 3]
				
			

Under the hood:

  • The array sort() function goes through the array and takes two consecutive elements under inspection.
  • It then subtracts the second element from the first element.
  • Then it places the first element before the second element if the result is positive.
  • It does this until the array is fully sorted.

Arrow Function Syntax

In JavaScript, a shortcut for writing a function is called an arrow function.

It has limitations and is not always effective. However, a good example of when you might use one is in a single expression function like the one in the sort() function.

Curly braces are optional if the function only has a single expression. Let’s use the arrow function to condense the code from the preceding example:

				
					var nums = [3, 1, 2];
nums.sort((a, b) => a - b);

console.log(numArray);
				
			

Output:

				
					[1, 2, 3]
				
			

Example 2. Sort in Ascending Order

Let’s look at another example where we order a list of numbers in ascending order before returning to the original issue.

Swap a and b in the comparison function to sort the list in ascending order instead:

				
					var nums = [3, 1, 2];
nums.sort((a, b) => b - a);

console.log(numArray);
				
			

Output:

				
					[3, 2, 1]
				
			

The Original Problem—How to Sort Array of Arrays in JavaScript

Let’s return to the first issue, which was how to sort an array of arrays.

Here is a case that you have already seen.

				
					const data = [["This", 3], ["just", 6], ["there", 12], ["is", 2]]

const sortedData = data.sort((a, b) => b[1] - a[1])

console.log(sortedData)
				
			

It is also simple to comprehend how this piece of code operates now that you know how fundamental sorting functions:

  • The sort() function applies a comparison function to the array that takes two arguments a and b. (The arrow syntax lets you omit the function and return keywords and the curly braces.)
  • This time a and b are not numbers, but arrays.
  • To sort the array of arrays, you need to specify based on which element you want to sort them.
  • Here we compare the arrays by their second elements.
  • Then the sort() function loops through the array of arrays and sorts it based on the magnitude of the second elements.

Wrap up

You just learnt how to modify the comparison function in Javascript to sort an array of arrays.  You also learnt how JavaScript’s fundamental sorting works.

				
					const data = [["This", 3], ["just", 6], ["there", 12], ["is", 2]]

const sortedData = data.sort((a, b) => b[1] - a[1])

console.log(sortedData)
				
			


Thanks for reading. Happy coding!