Skip to main content

並べ替え(sort).ホスト名順に(naturalorder,humanreadable)

Python

natsort ライブラリを使うか、自前で書く

py
def natsorted(items):
convert = lambda text: int(text) if text.isdigit() else text.lower()
sort_func = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(items, key = sort_func)

for val in natsorted(items):
print val

PHP

php
natsort(&$items)

TypeScript

ts
items.sort(naturalSort()); // npm install natural-sort @types/natural-sort

naturalSort() で関数を返してくれるので、カッコは必要。 必要の場合、カッコ内にオプションを指定。

ts
import naturalSort from 'natural-sort';

const items = ['host123', 'host54', 'host1', 'host12'];
items.sort(naturalSort());

Bash

bash
sort -V