Dsa
Sorting
Easy
Sort City Populations
You are tasked with sorting a list of cities along with their populations. Each city is represented by a string in the format 'CityName Population'. Your goal is to sort the cities by population in descending order and by name alphabetically for those with the same population.
Input:
- The first line contains an integer N (1 ≤ N ≤ 100) indicating the number of cities.
- The next N lines each contain a city name and its population separated by a space.
Output:
- A single line with the sorted cities, in the format 'CityName Population', separated by commas.
Example:
Input:
3
NewYork 8419600
LosAngeles 3980400
Chicago 2716000
Output:
NewYork 8419600, LosAngeles 3980400, Chicago 2716000
Key concepts
sortingliststuples
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now