Sort Array JavaScript

JavaScript sort() method; Through this tutorial, i am going to show you how to use JavaScript sort method.

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

JavaScript Sort Array

  • JavaScript sort() Syntax
  • Example 1: JavaScript sort array of numbers
  • Example 2: JavaScript Numeric array.sort() in asc order
  • Example 3: JavaScript Numeric array.sort() in desc order
  • Example 4: JavaScript String Array Sort
  • Example 5: JavaScript Extract minimum using array.sort()

JavaScript sort() Syntax

The following syntax reprenset the array.sort() method:

arr.sort([compareFunction])

Example 1: JavaScript sort array of numbers

This is the basic example of js array.sort() method is following:

 var arr = [5, 3, 10, 1, 6, 12]
document.write("Array before sorting = " + arr);
document.write("<br>");
document.write("Array After sorting = " + arr.sort());
Output
//// return
Array before sorting = 5,3,10,1,6,12
Array After sorting = 1,10,12,3,5,6

Example 2: JavaScript Numeric array.sort() in asc order

Sort an array in ascending order using the js regular function.

var arr = [5, 3, 10, 1, 6, 12]
document.write("Array before sorting = " + arr);
document.write("<br>");
arr.sort(function(a, b) {
return a - b;
});
document.write("Array After sorting = " + arr);
Output
return //// Sort an array ascending order
Array before sorting = 5,3,10,1,6,12
Array After sorting = 1,3,5,6,10,12

Example 3: JavaScript Numeric array.sort() in desc order

Sort an array in descending order using regular function.

var arr = [5, 3, 10, 1, 6, 12]
document.write("Array before sorting = " + arr);
document.write("<br>");
arr.sort(function(a, b) {
return b - a;
});
document.write("Array After sorting = " + arr);
Output
return //// Sort an array descending order:

Array before sorting = 5,3,10,1,6,12
Array After sorting = 12,10,6,5,3,1

When the sort() function compares two values, so it will always return true or false. We pass the array in javascript sort function than it will proccess and check, return sorted array.

Example 4: JavaScript String Array Sort

Now, you have an array and it holds string values, and you want to sort using array.sort() method.

Let’s take a look the following example:

 var arr = Array("php", "python", "javascript", "c", "c#", "rust");         
document.write("Array before sorting = " + arr);
document.write("<br>");
document.write("Array After sorting = " + arr.sort());
Output
return ///

Array before sorting = php,python,javascript,c,c#,rust
Array After sorting = c,c#,javascript,php,python,rust

Example 5: JavaScript Extract minimum using array.sort()

Extract a minimum value from an array.

 var arr = [5, 3, 10, 1, 6, 12]   
document.write("Array before sorting = " + arr);
document.write("<br>");
var result=arr.sort();
document.write("Minimum value of an array = " + arr[0]);
Output
//return

Array before sorting = 5,3,10,1,6,12
Minimum value of an array = 1

More JavaScript Tutorials

Recommended:-JavaScript Arrays

Leave a Comment