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
This article is written by plus2net.com team.
plus2net.com