In this article, you will learn about the JavaScript Array built-in method Array.prototype.sort()
. How does this method work in javascript?
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.
array.sort(compareFn(a, b));
This method takes 1 parameter which is optional:
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:
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]
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#"]
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"]
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]
Empty slots are moved to the end of the array.
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!
Comments