Question 15
Back to Exercise
What does the following JavaScript code print (console output, as in Node.js)?
const v = ["10", "9", "100", "9a"];
console.log(v.toSorted(), v.toSorted((a, b) => a.localeCompare(b, undefined, { numeric: true })));
- A [ '10', '100', '9', '9a' ] [ '9', '9a', '10', '100' ] Correct
- B [ '9', '9a', '10', '100' ] [ '9', '9a', '10', '100' ]
- C [ '10', '100', '9', '9a' ] [ '9', '10', '100', '9a' ]
- D [ '100', '10', '9a', '9' ] [ '9', '9a', '10', '100' ]
Correct Answer: Option A
Default sort compares character by character ("1" before "9"). The numeric option compares the digit runs as numbers: 9 < 10 < 100, and "9a" sorts right after "9".
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!