← Back to challenges

Sort Names According to the Length of Their Last Names

JavaScriptHardalgorithmscontrol_flowstringssorting

Instructions

Create a function that takes an array of names in the format "First Name Last Name" (e.g. "John Doe"), and returns an array of these names sorted by the length of their last names. If the length of multiple last names are the same, then proceed to sort alphabetically by last name.

Examples

lastNameLensort([
  "Jennifer Figueroa",
  "Heather Mcgee",
  "Amanda Schwartz",
  "Nicole Yoder",
  "Melissa Hoffman"
]) ➞ ["Heather Mcgee", "Nicole Yoder", "Melissa Hoffman", "Jennifer Figueroa", "Amanda Schwartz"]

Notes

If last names are of the same length, sort alphabetically by last name.

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