Sorting Using JavaScript Array Sort() Method

Sorting Using JavaScript Array Sort() Method

The JavaScript sort() method, here you will learn about javascript sort() method. How it work with string, numbers and character. Javascript sort method is used to sort the elements of an array and return new array.

Array sort method is used to sort JavaScript Array if manual code will be written to write the array, it will take a long time.

The javaScript Sort () function defines the value as strings. It works well for the strings. We will sort the array of element or items.

JavaScript Array Sort()

  • JavaScript sort() Syntax
  • Examples

JavaScript sort() Syntax

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

arr.sort([compareFunction])

Example 1: Basic array.Sort

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: 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: 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: 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: 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

You may like

  1. JavaScript Array splice() Method By Example
  2. JavaScript: Clone an Array & Object By Examples
  3. JavaScript: Convert String to Array JavaScript
  4. Remove (First, last, specific) Element From Array Javascript
  5. javaScript Push() Array By Example
  6. javaScript Push Array, Items Into Array Example
  7. JavaScript Check the Object | isArray() Function
  8. JavaScript: Important Array Methods

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

Leave a Reply

Your email address will not be published. Required fields are marked *