← Back to challenges

Sort By Index of Character

JavaScriptHardsortingarrays

Instructions

Write a function that sorts an array of characters alphabetically in ascending order (a-z) by the character at the n-th character.

Examples

sortByCharacter(["az16", "by35", "cx24"], 2) ➞ ["cx24", "by35", "az16"]
// By 2nd character: ["x", "y", "z"] is in alphabetical order.

sortByCharacter(["mayor", "apple", "petal"], 5) ➞ ["apple", "petal", "mayor"]
// By 5th character: ["e", "l", "r"] is in alphabetical order.

sortByCharacter(["mate", "team", "bade"], 3) ➞ ["team", "bade", "mate"]

Notes

All inputs will be of same length.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.