-1

Perhaps we have an array of arrays such as this:

["a", "11.36"]
["b", "12.13"]
["c", "11.00"]

Is there a simple function to be able to sort them based on the 2nd index?

Nicholas Hazel
  • 3,758
  • 1
  • 22
  • 34
  • I know how to solve it... I just wanted an absurdly easy question that people can reference because it took me too long to research. – Nicholas Hazel Jul 17 '19 at 08:11
  • Fair enough. I just wanted to post something I struggled to find with some verbiage I wasn't using. I'll delete it in the morning if necessary. – Nicholas Hazel Jul 17 '19 at 08:27

2 Answers2

0

With use of Array.prototype.sort() and Destructuring assignment it would look like this

const a = [
  ["a", "11.36"],
  ["b", "12.13"],
  ["c", "11.00"]
];

a.sort(([,a], [,b]) => a - b)
console.log(a)
jank
  • 840
  • 4
  • 7
0

You can try this:

let arr = [["a", "11.36"], ["b", "12.13"], ["c", "11.00"]]
arr = arr.sort(function(a,b) {
    return a[1] - b[1];
});
    
console.log(arr)
mitesh7172
  • 666
  • 1
  • 11
  • 21