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

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

javascript - Confirm a form & display message if form is valid with JQuery -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -