JavaScript Array Sort using Sort() Method

The JavaScript sort() method, In this tutorial, you will learn about the 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.

Sort Array javaScript

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

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

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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