Natural order in python

import re


def nautural_order_key(s, _split=re.compile(r'(\d+)').split, conv=int):
    it = _split(s)
    it[1::2] = map(conv, it[1::2])
    return it

x = ['hoge99', 'hoge100', 'hoge10']
x.sort(key= nautural_order_key)
print(x)  # ['hoge10', 'hoge99', 'hoge100']

Recommended Posts

Natural order in python
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 18 in Python
Singleton pattern in Python
File operations in Python
Key input in Python
Daily AtCoder # 33 in Python
Logistic distribution in Python
Daily AtCoder # 7 in Python
LU decomposition in Python
One liner in Python
[Python] Sort the list of pathlib.Path in natural sort
Daily AtCoder # 24 in Python
case class in python
RNN implementation in python
Daily AtCoder # 8 in Python
File processing in Python
Elasticsearch Reindex in Python
Daily AtCoder # 42 in Python
Basic sorting in Python