SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

Sorting of array in JavaScript

We can sort elements of an array by using sort() function in JavaScript. This sort() function once applied changes the positions of the elements and arrange them in alphabetical order. Here is the basic syntax of the sort function.

Array.sort();


Now let us try to create an array and then display them to see the elements and their order. After this we will apply sort() function and then again we will display the elements of the array to check the order.

Here is the code.

var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
for (i=0;i<scripts.length;i++)
{
document.write(scripts[i] + "<br >");
}
document.write("<br>-----<br>");

scripts=scripts.sort();
for (i=0;i<scripts.length;i++)
{
document.write(scripts[i] + "<br >");
}


We can define our sorting algorithm in a function and JavaScript will use the function to sort the array.

There are some condition the function must satisfy. Here they are

The function will accept two arguments and these arguments are to be compared for the algorithm. The function will return numbers based on the comparison of the two arguments.
If the returned number is < 0 then first argument should appear before the second argument.

If the returned number is greater than 0 then first argument should appear after the second argument.

If the returned number is equal to 0 then both the arguments should be equal.

Let us try to sort the array elements by their lengths using the function. Here is the code

function bylength(a1,a2){
if(a1.length < a2.length)
return -1;
if(a1.length > a2.length)
return 1;
if(a1.length == a2.length)
return 0;

}

var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
for (i=0;i<scripts.length;i++)
{
document.write(scripts[i] + "<br >");
}
document.write("<br>-----<br>");

scripts=scripts.sort(bylength);
for (i=0;i<scripts.length;i++)
{
document.write(scripts[i] + "<br >");
}



Further readings
Declaring array in JavaScript
Displaying elements of an array by looping through
join():Displaying elements of an array using join() function
sort():Sorting of elements of an array using function
length:Length property to get the total number of elements in an array
reverse():Reversing the elements of an array
pop():Removeing last element of an array by using pop()
shift():Removeing first element of an array using shift()
push():Adding elements to array using push()
unshift():Adding elements to array using unshift()
splice():Add replace remove elements from an array using splice()
split():Creating array by splitting string variable
toString: To join all elements and create a string
concat: To join two or more arrays to a single array
slice: To return element from an array with starting and ending positions
Two Dimensional Array: Adding and displaying elements




Join Our Email List
Email:  
For Email Newsletters you can trust
Array Functions