var arr = [
{ price: 10, amount: 10},
{ price: 12, amount: 12},
{ price: 9, amount: 7},
...
];
var arr = [
{ price: 10, amount: 10 },
{ price: 12, amount: 12 },
{ price: 9, amount: 7 },
{ price: 17, amount: 8 },
{ price: 4, amount: 2 }
]
arr
.sort(function(a, b) {
return a.price - b.price
})
.reverse()
.map(function(item, i) {
item.index = i + 1
})
console.log(arr)
/*
[
{ price: 17, amount: 8, index: 1 },
{ price: 12, amount: 12, index: 2 },
{ price: 10, amount: 10, index: 3 },
{ price: 9, amount: 7, index: 4 },
{ price: 4, amount: 2, index: 5 }
]
*/
var sorted = products.sort((curr, prev)=>{
if (curr.price > prev.price) return 1
if (curr.price < prev.price) return -1
return 0
})
https://jsfiddle.net/0v0vhrqu/