1

I have a multi dimensional array that has 3 elements in each index.

Example [[bob,12,white],[alex,2,hispanic]]

How can I sort the array by the integer age (middle value)?

1 Answer 1

1

Take a look at Array.prototype.sort. You can pass a function to determine how your array should be sorted.

const sorted = [
  ['bob',12,'white'],
  ['alex',2,'hispanic']
].sort((a, b) => a[1] - b[1]) // sorts by integer age asc

console.log(sorted)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.