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

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -