javascript - Sorting array of numbers -
i have things pushed array (down below) , sort item(s) item price. tried using sort doesn't work me. after loop array , retrieve item name , item price.
pushing item array: array.push(itemname,itemprice)
trying use sort function:
array.sort(function(a,b){ return b[1] - a[1] });
edit: agree nina's comment. changing compare function
looks need array of objects , define own comparison function sorting.
so need add element array this:
array.push({itemname:name,itemprice:price}) define custom comparison function
function compare(a,b) { return a.itemprice-b.itemprice; } use custom function
array.sort(compare)
Comments
Post a Comment