There was a case where I wanted to sort the array of character strings by word length and order them in alphabetical order, so I will leave it as a memo.
words = ["Nihon", "America", "Russia", "France", "Italy", "China", "Singapore", "Australia", "Want", "Dust", "When", "England"]
words = sorted(words, key=lambda word: (len(word), word))
print(words)
The output will look like this:
['Want', 'Dust', 'When', 'Nihon', 'Russia', 'America', 'England', 'Italy', 'France', 'China', 'Singapore', 'Australia']
It's easy. Please let me know if there is a better way.
Recommended Posts