Sort Objects by a Field
Updated
Problem statement
A Person record has four fields, always in this order: firstName, lastName, age and ssn (a U.S. Social Security number). Each entry of people is one person, written as a list of four strings that hold these fields.
The string field names one of the four fields. Return the people sorted by their value in that field, from smallest to largest:
firstNameandlastNameare compared in alphabetical order.ageis compared as a number, so"9"comes before"27".- Every
ssnis written asddd-dd-dddd(3 digits, a dash, 2 digits, a dash, 4 digits), so comparing two of them as text gives their numeric order.
When two people have the same value in that field, keep them in the order they had in people. A sort that keeps equal items in their original order is called stable.
Function
sortByField(people: List<List<String>>, field: String) → List<List<String>>Examples
Example 1
people = [["Maya","Chen","34","912-34-5678"],["Liam","Ortiz","9","905-11-2233"],["Ava","Brooks","34","931-20-4455"],["Noah","Kim","27","948-02-7719"]]field = "age"return = [["Liam","Ortiz","9","905-11-2233"],["Noah","Kim","27","948-02-7719"],["Maya","Chen","34","912-34-5678"],["Ava","Brooks","34","931-20-4455"]]As numbers, the ages are 9, 27, 34 and 34, so Liam comes first even though the text "9" would sort after "27". Maya and Ava are both 34, so they stay in their input order: Maya, then Ava.
Example 2
people = [["Zoe","Smithers","41","977-50-1203"],["Eli","Smith","29","963-08-3341"],["Ian","Baker","35","920-77-0019"]]field = "lastName"return = [["Ian","Baker","35","920-77-0019"],["Eli","Smith","29","963-08-3341"],["Zoe","Smithers","41","977-50-1203"]]Baker comes first. "Smith" is the start of "Smithers", and a shorter text that is the start of a longer one comes first, so Eli Smith comes before Zoe Smithers.
Example 3
people = [["Omar","Reyes","52","941-22-0001"],["Lena","Park","19","903-99-9999"]]field = "ssn"return = [["Lena","Park","19","903-99-9999"],["Omar","Reyes","52","941-22-0001"]]The first 3 digits already differ: 903 is less than 941, so Lena comes first.
Constraints
1 <= people.length <= 200people[i].length == 4- Each
firstNameandlastNamehas 1 to 10 English letters: an uppercase first letter, then lowercase letters. - Each
ageis a whole number from0to120, written without leading zeros. - Each
ssnhas the formddd-dd-dddd, where everydis a digit, and no two people share anssn. fieldis one of"firstName","lastName","age"and"ssn".
More Zillow interview questions
- Cumulative Fibonacci SumOA · PHONE SCREEN · Seen Aug 2021
- First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
- Reverse StringOA · PHONE SCREEN · Seen Sep 2019
- Mastermind Guess ScoreOA · PHONE SCREEN · Seen Jun 2019
- Convert a String to a LongOA · Seen Jan 2018
- Insert and Delete in a Trinary TreeOA · Seen May 2016
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
- Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Top N Longest SentencesDeloitte · OADeloitte interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Frequently Bought TogetherAmazon · OA
- Aggregate Paginated Project Run RecordsReplit · OA
- Product Record SearchWells Fargo · OAWells Fargo interview questions
- Sort Ancestral NamesRoblox · OARoblox interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions