[Javascript] Array Numerical Sorting

Flávio Gomes flavio at economisa.com.br
Thu Nov 30 11:20:30 CST 2006


I was trying to sort an array numerically and couldnt make it untill I 
found it on this website:

http://javascript.about.com/library/blsort.htm

Example below:

a = new Array();

for (x=0;x<7;x++)
{ a[x] = Math.ceil ( 12 * Math.random() );
}

document.write('<br>');

function numOrdAsc(a, b){ return (a-b); }
function numOrdDesc(a, b){ return (b-a); }

a.sort( numOrdAsc );
document.write(Ascending : ' + a + '<br>');

a.sort( numOrdDesc );
document.write('Descending : ' + a + '<br>');

Hope it to help someone =)

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:sort 
<-- best reference found


-- 
Flavio Gomes
flavio at economisa.com.br




More information about the Javascript mailing list