;

Javascript Array - sort() Method


Tutorialsrack 05/11/2022 Jquery Javascript

In this article, you will learn about the JavaScript Array built-in method Array.prototype.sort(). How does this method work in javascript? 

Array.prototype.sort()

The Array.prototype.sort() method is used to return the element of the given array in sorted order. And the default order is ascending, which is built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.

Syntax
array.sort(compareFn(a, b));

This method takes 1 parameter which is optional:

  • compareFn(Optional): This parameter holds the compare function that defines the sort order. If this parameter is omitted, then the array elements are converted to strings, then sorted according to each character's Unicode code point value. And this compareFn takes 2 parameters as given below: 
    • a: The first element for comparison.
    • b: The second element for comparison.

When you are not using compare function, all undefined elements are sorted to the end of the array. And this sort() method overwrites the original array.

Here are some examples of Array.prototype.sort() method:

Example 1: Sorting an Array Without Using Compare Function

Example 1: Sorting an Array Without Using Compare Function
const stringArray = ["Java", "C#", "Go", "JavaScript", "Python"]

const numberArray = [42, 5, 60, 75, 10, 100, 1000, 50, 30, 20, 25];


//Sorting a string Array without using compare function
console.log(stringArray.sort())
//Output => ["C#", "Go", "Java", "JavaScript", "Python"]

//Sorting a number array without using compare function
//when you sort a number array without compare function, 
//element treated as string and sorted in a alphabetical order
console.log(numberArray.sort())
//Output => [10, 100, 1000, 20, 25, 30, 42, 5, 50, 60, 75]

Example 2: Sorting an Array Using Compare Function

Example 2: Sorting an Array Using Compare Function
const stringArray = ["Java", "C#", "Go", "JavaScript", "Python"]

//Sorting a string Array using compare function
//in ascending order
function sortStringArrayAsc(a, b){
  return a.localeCompare(b);
}

console.log(stringArray.sort(sortStringArrayAsc));
//Output => ["C#", "Go", "Java", "JavaScript", "Python"]


//Sorting a string Array using compare function
//in descending order
function sortStringArrayDesc(a, b){
  return b.localeCompare(a);
}

console.log(stringArray.sort(sortStringArrayDesc));
//Output => ["Python", "JavaScript", "Java", "Go", "C#"]

Example 3: Sorting an Array on the Basis of Element Length Using Compare Function

Example 3: Sorting an Array on the Basis of Element Length Using Compare Function
const stringArray = ["Java", "C#", "Go", "JavaScript", "Python"]


//Sorting an array on the basis of the length of the element using the compare function
//in ascending order
function sortStringArrayOnlengthAsc(a, b){
  return a.length - b.length;
}

console.log(stringArray.sort(sortStringArrayOnlengthAsc));
//Output => ["C#", "Go", "Java", "Python", "JavaScript"]


//Sorting a string Array on the basis of element length using the compare function
//in descending order
function sortStringArrayOnLengthDesc(a, b){
  return b.length - a.length;
}

console.log(stringArray.sort(sortStringArrayOnLengthDesc));
//Output => ["JavaScript", "Python", "Java", "C#", "Go"]

Example 4: Sorting a Numeric Array with Compare Function

Example 4: Sorting a Numeric Array with Compare Function
const numberArray = [42, 5, 60, 75, 10, 100, 1000, 50, 30, 20, 25];



//Sorting an array using the compare function
//in ascending order
function sortNumberArrayAsc(a, b){
  return a - b;
}

console.log(numberArray.sort(sortNumberArrayAsc));
//Output => [5, 10, 20, 25, 30, 42, 50, 60, 75, 100, 1000]


//Sorting a string Array using the compare function
//in descending order
function sortNumberArrayDesc(a, b){
  return b - a;
}

console.log(numberArray.sort(sortNumberArrayDesc));
//Output => [1000, 100, 75, 60, 50, 42, 30, 25, 20, 10, 5]

Example 5: Sorting sparse arrays

Empty slots are moved to the end of the array. 

Example 5: Sorting sparse arrays
console.log(["Java", "C#", , "JavaScript", "Python"].sort());
//Output => ['C#', 'Java', 'JavaScript', 'Python', empty]


console.log(["Java", "C#", , "JavaScript", undefined, "Python"].sort());
//Output => ['C#', 'Java', 'JavaScript', 'Python', undefined, empty]

I hope this article will help you to understand the javascript Array built-in method Array.prototype.sort()

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags