[Solved] Sort index of array based on value [closed]


  • Sort an mapped array based on value.
  • Find the index of value based on the index from sorted array.
var obj = [{
  index: 1,
  value: 20
}, {
  index: 2,
  value: 40
}, {
  index: 3,
  value: 10
}, {
  index: 4,
  value: 50
}];
var sortedArr = obj.map(function(el) {
  return el.value;
}).sort(function(a, b) {
  return b - a
});
var mapped = obj.map(function(el) {
  var index = sortedArr.indexOf(el.value) + 1;
  el.index = index;
  return el;
});
console.log(JSON.stringify(mapped,null,4));

solved Sort index of array based on value [closed]