Sort Ancestral Names
Problem statement
Each entry in names has the form givenName romanNumeral, separated by one space. Sort the entries first by given name in ascending alphabetical order, then, for equal given names, by the Roman numeral’s numeric value in ascending order.
Return the original full strings in their sorted order, keeping each Roman spelling. For this exercise, assume the input array must remain unchanged.
Valid Roman numerals use I, V, X and L in standard notation: IV = 4, IX = 9 and XL = 40.
Function
sortRoman(names: String[]) → String[]Examples
Example 1
names = ["Louis IX","Louis VIII"]return = ["Louis VIII","Louis IX"]Both given names are Louis; 8 precedes 9.
Example 2
names = ["Philippe I","Philip II"]return = ["Philip II","Philippe I"]Philip alphabetically precedes Philippe, regardless of numeral values.
Example 3
names = ["Steven XL","Steven XVI","David IX","Mary XV","Mary XIII","Mary XX"]return = ["David IX","Mary XIII","Mary XV","Mary XX","Steven XVI","Steven XL"]Sort David, Mary, Steven, then each name’s numeric ordinal.
Constraints
1 <= names.length <= 50; every full entry is distinct.- Every entry contains exactly one space between the given name and numeral.
1 <= givenName.length <= 20; its first letter is uppercaseA-Zand any following letters are lowercasea-z.- Every Roman numeral is valid standard notation for an integer from
1through50, inclusive.