How to use Compare Function In Javascript

We can create a Compare function that returns negative, zero, or positive values. 

Syntax:  

function(a, b){return a - b}
  • Negative Value ( a < b) => a will be placed before b
  • zero value (a == b) => No Change
  • Positive Value (a > b ) => a will be placed after b

Example: This example uses compare function to sort the array elements in ascending order. 

javascript
// Declare and initialize an Array
let marks = [12, 25, 31, 23, 75, 81, 100];

// Print Before sorting array 
console.log("Original Array");
console.log(marks);

// Sort elements using compare method 
marks.sort(function (a, b) { return a - b });

console.log("After sorting in Ascending order");

// Print sorted Numeric array 
console.log(marks);         

Output
Original Array
[
  12, 25,  31, 23,
  75, 81, 100
]
After sorting in Ascending order
[
  12, 23,  25, 31,
  75, 81, 100
]

Now, we would like to sort the array in Descending order then we have to change the compare function.

Syntax:  

function(a, b){return b - a}
  • Negative Value ( b < a) => a will be Placed after b
  • zero value (a == b) => No Change
  • Positive Value (b > a ) => a will be placed before b

Example: This example uses compare function to sort the array elements in descending order. 

javascript
// Declare and initialize an Array
let marks = [12, 25, 31, 23, 75, 81, 100];

// Print Before sorting array 
console.log("Original Array");
console.log(marks);

// Sort elements using compare method 
marks.sort(function (a, b) { return b - a });

console.log("After sorting in Descending order");

// Print sorted Numeric array 
console.log(marks);        

Output
Original Array
[
  12, 25,  31, 23,
  75, 81, 100
]
After sorting in Descending order
[
  100, 81, 75, 31,
   25, 23, 12
]

How to Sort Numeric Array using JavaScript ?

In JavaScript, where arrays and their manipulation form the backbone of many operations. One such operation is sorting, and JavaScript provides a built-in method, Array.sort(), to help us with that.

Now, here’s a fun fact: Array.sort() sorts the elements in place and returns the sorted array, but it does so in string format. Yes, you heard it right! It treats every element as a string while sorting. This works perfectly fine for arrays of strings, but when it comes to numbers, things can get a bit tricky.

For instance, if numbers are sorted as strings, “75” is considered larger than “200”. Surprising, isn’t it? But don’t worry, we’ve got you covered. In this article, we’ll dive deep into the workings of Array.sort(), and explore how to effectively sort both string and number arrays. So, let’s set the some examples.

Example 1: In this example, the Array sort() method is implemented. The array is sorted as a string.

Javascript
let arr = [12, 25, 31, 23, 75, 81, 100]
console.log(arr.sort())

Output
[
  100, 12, 23, 25,
   31, 75, 81
]

Example 2: This example sorts the array elements in string format. 

javascript
// Declare and initialize original array
let marks = [12, 25, 31, 23, 75, 81, 100];

// Print Before Sorting Array 
console.log("Original Array");
console.log(marks);

// Call sort function
marks.sort();

console.log("After Sorting in Ascending Order");

// Print Sorted Numeric Array 
console.log(marks);        

Output
Original Array
[
  12, 25,  31, 23,
  75, 81, 100
]
After Sorting in Ascending Order
[
  100, 12, 23, 25,
   31, 75, 81
]

Then, how to sort number array elements? 

There are two approaches to sorting the number array in ascending order:

Table of Content

  • Using Compare Function
  • Creating Loops
  • Using Array.prototype.sort with parseInt

Similar Reads

Using Compare Function

We can create a Compare function that returns negative, zero, or positive values....

Creating Loops

We can also use loops to sort the array elements. Here, we will use bubble sort (a simple sorting technique) to sort the array elements in ascending order....

Using Array.prototype.sort with parseInt

Using Array.prototype.sort with parseInt, you can sort a numeric array containing string numbers by converting them to integers during comparison. This ensures proper numerical sorting, rather than lexicographical sorting based on string values....