Thursday, August 11, 2016

Javascript Sorting Algo

Javascript Sorting Algo

var arrObj = new Array(); // or [];

var temp = new Object();
temp.num = 3.58;
arrObj.push(temp);  //add more of this; can loop through array of objects
....
arrObj.sort(sortByObjNumber);

function sortByObjNumber(a, b)
{
var a = parseInt(a.num);
var b = parseInt(b.num);

if(a < b)
{
return -1;
}
else if(a == b)
{
return 0;
}
else
{
return 1;
}
}

No comments:

Post a Comment