Sort the string array in order of length & Japanese syllabary

Introduction

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.

Solution

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

Sort the string array in order of length & Japanese syllabary
I want to sort a list in the order of other lists
[Python] Sort the list of pathlib.Path in natural sort
Sort the elements of the array by specifying the conditions
Output in the form of a python array
Get the last element of the array by splitting the string in Python and PHP
Store Japanese (multibyte character string) in sqlite3 of python
[Golang] Specify an array in the value of map
About the uncluttered arrangement in the import order of flake8
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
Find out the apparent width of a string in python
About the garbled Japanese part of pandas-profiling in Jupyter notebook
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
Make the function of drawing Japanese fonts in OpenCV general
Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
Process the contents of the file in order with a shell script
[python] Get the rank of the values in List in ascending / descending order
Sort the file names obtained by Python glob in numerical order
Japanese translation of the e2fsprogs manual
The story of participating in AtCoder
The story of the "hole" in the file
Japanese translation of the man-db manual
The meaning of ".object" in Django
Japanese translation of the util-linux manual
Japanese translation of the iproute2 manual
Get the query string (query string) in Django
Click the Selenium links in order to get the elements of individual pages
[Introduction to Python] Thorough explanation of the character string type used in Python!
How to sort by specifying a column in the Python Numpy array.
What's in the parameter? Edit String & Expression
Conversion of string <-> date (date, datetime) in Python
Change the length of Python csv strings
Check the behavior of destructor in Python
The story of an error in PyOCR
Try using the HL band in order
Comparison of Japanese conversion module in Python3
Implement part of the process in C ++
Omit BOM from the beginning of the string
Arbitrarily rearrange the column order of Pandas.DataFrame
Japanese translation of the LDP man-pages manual
The result of installing python in Anaconda
Let's claim the possibility of pyenv-virtualenv in 2021
I wrote the selection sort in C
R: Use Japanese instead of Japanese in scripts
The basics of running NoxPlayer in Python
Escape curly braces in the format string
Change the order of PostgreSQL on Heroku
In search of the fastest FizzBuzz in Python
[PowerShell] Get the reading of the character string
The backslash of the Japanese keyboard is "ro"
[Python] Combine all the elements in the array
Japanese translation: PEP 20 --The Zen of Python
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
[Python] Summary of functions that return the index that takes the closest value in the array
Use the Java SDK of GoogleMapsAPI to get the result of reverse GeoCoding in Japanese.
Divides the character string by the specified number of characters. In Ruby and Python.
Let Code Day1 Starting from Zero "1389. Create Target Array in the Given Order"
[python] How to sort by the Nth Mth element of a multidimensional array