Demo of Sorting of array in ascending and descending order

Ascending Order ( output )

<script>
function bylength(a1,a2){
if(a1 < a2)
return -1;
if(a1 > a2)
return 1;
if(a1 == a2)
return 0;
}
var no_array = new Array(2, 22, 4, 24, 32);

var no_array2=no_array.sort(bylength);
for (i=0;i<no_array2.length;i++)
{
document.write(no_array2[i] + "<, >");
}
</script>

Descending Order (output)

<script>
function bylength(a2,a1){
if(a1 < a2)
return -1;
if(a1 > a2)
return 1;
if(a1 == a2)
return 0;
}
var no_array = new Array(2, 22, 4, 24, 32);

var no_array2=no_array.sort(bylength);
for (i=0;i<no_array2.length;i++)
{
document.write(no_array2[i] + "<, >");
}
</script>
Tutorial on Array Sort . Demo on sorting on alphabetical order
Array Reference How to display elements of an Array
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer