Edabit

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.

Solution:

 1  2  3  4  5  6  7  8  9 10
function lastNameLensort(names) {   return names.sort((a, b) => {     let first = a.split(" ")[1];     let second = b.split(" ")[1];     if (first.length == second.length) {       return first.localeCompare(second);     }     return first.length - second.length;   }); } 

This free site is ad-supported. Learn more