Does not matter what language you are working on, you will always find Sorting as an essential part of the language. In this post we will focus on Sorting in JavaScript. Sorting is a technique to arrange elements in certain order.

Let us suppose you have an array as following,

image

You will get array elements printed as following

image

If you want to sort this array, JavaScript allows you to sort an array very simply by calling sort function on array. See the listing below, 

image

And now when you print array elements you will elements sorted in alphabetical order 

image

Code for reference is following,


<span style="font-size: 14pt;"></span>

<script type="text/javascript" >

<span style="font-size: 14pt;"></span>

var fruits = ["mango", "lichi", "banana", "Apple"];
 console.log("Before Sorting ");
 for (var i = 0 ; i < fruits.length ; i++) {

<span style="font-size: 14pt;"></span>

console.log(fruits[i]);
 }

 fruits.sort();

<span style="font-size: 14pt;"></span>

console.log("After Sorting");
 for (var i = 0 ; i < fruits.length ; i++) {

<span style="font-size: 14pt;"></span>

console.log(fruits[i]);
 }

 </script>

<span style="font-size: 14pt;"></span>

Let us assume you have an integer array then you can sort that as following


<span style="font-size: 12pt;"></span>

var marks = [89, 45, 67, 39, 99, 58, 44, 92];
 var sortedmarks = marks.sort();
 for (var i = 0 ; i < sortedmarks.length ; i++) {

<span style="font-size: 12pt;"></span>

console.log(sortedmarks[i]);
 }

<span style="font-size: 12pt;"></span>

You will get sorted array as following

image

But wait for a while let us assume array as following

image

Now if you try to sort this array, you will get sorted order as following. This sort order is not expected

image

image

You can solve this problem by passing array reference. JavaScript allows you to pass a function reference as input parameter of sort function.

image

To sort a numeric array, let us create a Compassion function and pass as input parameter to sort method. Compare method is as following

image

And now pass this method to sort function

image

Sorted array would be as following

image

If you want to sort numeric array in descending order you can do that by modifying compare method as following

image

For your reference to code to sort an integer array is given below

<script type="text/javascript" >

var marks = [714,89, 45, 67, 39, 99, 58, 44, 92,100,234];
 var sortedmarks = marks.sort(CompareA);
 for (var i = 0 ; i < sortedmarks.length ; i++) {

console.log(sortedmarks[i]);
 }
 var sortedmarksD = marks.sort(CompareD);
 for (var i = 0 ; i < sortedmarksD.length ; i++) {

console.log(sortedmarksD[i]);
 }

function CompareD(a,b)
 {
 return b-a;
 }

function CompareA(a, b) {
 return a - b;
 }

 </script>

In this way you can work with sorting of Array in JavaScript. I hope you find this post useful. Thanks for reading.

Advertisements

One thought on “Sorting of JavaScript Array

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s