-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?

2
  • 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. Commented Jul 17, 2019 at 8: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. Commented Jul 17, 2019 at 8:27

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.