人間にとって自然なソート [Tcl]
ファイル名の連番で "z100.html" が "z2.html" よりも前に来るみたいな問題を解決するためのソートアルゴリズムをいろんな言語で実装したページ [1] があった。最近の Windows のエクスプローラでも自然な順序でソートしてくれる(いつのまにかだけどいつからだろう?)。
これって Tcl だと以下のように lsort のオプションで一発なんです。
set l [list "1000X Radonius Maximus" "10X Radonius" "200X Radonius" "20X Radonius" "20X Radonius Prime" "30X Radonius" "40X Radonius" "Allegia 50 Clasteron" "Allegia 500 Clasteron" "Allegia 51 Clasteron" "Allegia 51B Clasteron" "Allegia 52 Clasteron" "Allegia 60 Clasteron" "Alpha 100" "Alpha 2" "Alpha 200" "Alpha 2A" "Alpha 2A-8000" "Alpha 2A-900" "Callisto Morphamax" "Callisto Morphamax 500" "Callisto Morphamax 5000" "Callisto Morphamax 600" "Callisto Morphamax 700" "Callisto Morphamax 7000" "Callisto Morphamax 7000 SE" "Callisto Morphamax 7000 SE2" "QRS-60 Intrinsia Machine" "QRS-60F Intrinsia Machine" "QRS-62 Intrinsia Machine" "QRS-62F Intrinsia Machine" "Xiph Xlater 10000" "Xiph Xlater 2000" "Xiph Xlater 300" "Xiph Xlater 40" "Xiph Xlater 5" "Xiph Xlater 50" い"Xiph Xlater 500" "Xiph Xlater 5000" "Xiph Xlater 58"]
foreach i [lsort -dictionary $l] {
puts $i
}
実行結果:
D:\>tclsh alphanum.tcl 10X Radonius 20X Radonius 20X Radonius Prime 30X Radonius 40X Radonius 200X Radonius 1000X Radonius Maximus Allegia 50 Clasteron Allegia 51 Clasteron Allegia 51B Clasteron Allegia 52 Clasteron Allegia 60 Clasteron Allegia 500 Clasteron Alpha 2 Alpha 2A Alpha 2A-900 Alpha 2A-8000 Alpha 100 Alpha 200 Callisto Morphamax Callisto Morphamax 500 Callisto Morphamax 600 Callisto Morphamax 700 Callisto Morphamax 5000 Callisto Morphamax 7000 Callisto Morphamax 7000 SE Callisto Morphamax 7000 SE2 QRS-60 Intrinsia Machine QRS-60F Intrinsia Machine QRS-62 Intrinsia Machine QRS-62F Intrinsia Machine Xiph Xlater 5 Xiph Xlater 40 Xiph Xlater 50 Xiph Xlater 58 Xiph Xlater 300 Xiph Xlater 500 Xiph Xlater 2000 Xiph Xlater 5000 Xiph Xlater 10000
Perl や Python の実装が用意されてるってことはこれらの言語にはこの機能はないってことなんだろうな。Perl にはあってもおかしくなさそうだとおもったけど。
以上 Tcl 自慢ができて満足でした。
[1] http://www.davekoelle.com/alphanum.html
コメント 0