lsort

ReferenceTOPKeywords

コマンド名

lsort - リストの要素をソートします。

構文

lsort ?options? list

解説

本コマンドはlist の要素をソートし、ソートされた順番の新しいリストを返します。lsortコマンドはマージ・ソートアルゴリズムを使います。これは"O(n log n)"のパフォーマンス特性を持つ安定したソートです。

デフォルトではASCIIソートが使われ、結果は昇順で返されます。しかし、ソート処理を制御するために以下のオプショ ンのいずれかがlistの前に指定できます(区別できれば省略形も許されます)。

-ascii
-dictionary
-integer
-real
-command command 
-increasing
-decreasing
-index index 
-unique
-ascii
ASCII照合順で文字列比較が使用されます。 これがデフォルトです。

-dictionary
辞書順比較が使用されます。これは次の点を除いて-asciiと同じです。(a) tie-breakerを除いて大文字小文字は無視されます。(b)2つの文字列が埋め込まれた数字を含んでいる場合、数字は文字としてではなく、整数として比較されます。例えば、-dictionaryモードではbigBoybigbangbigboyの間にソートされます。x10yx9y x11yの間にソートされます。

-integer
リスト要素を整数に変換して、整数比較を使用します。

-real
リスト要素を浮動小数点値に変換して浮動小数点数比較を使用します。

-commandコマンド
commandを比較コマンドとして用います。 2つの要素を比較するため、commandと、追加引数として後につく2つの要素から成るTcl スクリプトを構成し、評価されます。スクリプトは、最初の要素が二番目より小さい場合0未満を返し、等しい場合0を、大きい場合0以上の整数を返します。

-increasing
リストを昇順にソートします("最小"のものが一番目)。このオプションはデフォルトです。

-decreasing
リストを降順にソートします("最大"のものが一番目)。

-index index
このオプションが指定された場合、listの各要素自身は適切なTclのサブリストでなければなりません。このオプションが指定された場合、サブリスト全体に基づくソートの代わりにlsortは各サブリストのindex番目の要素を抽出し、与えられた要素に基づいてソートします。キーワードendはサブリストの最後の要素をソートするのためのindexとして指定できます。end-index はサブリストの最後からオフセットを引いた位置の要素をソートします。例えば
lsort -integer -index 1 {{First 24} {Second 18} {Third 30}}

{Second 18} {First 24} {Third 30}を返します。そして、

lsort -index end-1 {{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}}

{ c 4 5 6 d h } {a1 e i } { b 2 3 f g } を返します。このオプションは-commandを使うより、同じ効果 を成し遂げるために一層効率的です。

-unique
本オプションが指定された場合、リストに発見された重複要素の最後のセットのみが保持されます。注意、重複が判断されるのはソートに使われる比較方法によります。したがって、-index 0が使われると{ 1 a }{ 1 b }が重複と判断され、2番目の要素{ 1 b }が保持されます。

注意事項

lsortのオプションはコントロールするために使われる比較手法です。実際の値を影響するものではありません。ソートされるリストが2つ以下の要素を持つ場合のみ、この区別 が可能です。

lsortコマンドはネスト可能です。-commandオプションに使われるコマンドの一部としてlsortが使用できます。

ASCII順を使って、リストをソートします

% lsort {a10 B2 b1 a1 a2}
B2 a1 a10 a2 b1

Dictionary順を使ってリストをソートします

% lsort -dictionary {a10 B2 b1 a1 a2}
a1 a2 a10 b1 B2

整数リストをソートします

% lsort -integer {5 3 1 2 11 4}
1 2 3 4 5 11
% lsort -integer {1 2 0x5 7 0 4 -1}
-1 0 1 2 4 0x5 7

浮動小数点数リストをソートします

% lsort -real {5 3 1 2 11 4}
1 2 3 4 5 11
% lsort -real {.5 0.07e1 0.4 6e-1}
0.4 .5 6e-1 0.07e1

インデックスでソートします

% # Note the space character before the c
% lsort {{a 5} { c 3} {b 4} {e 1} {d 2}}
{ c 3} {a 5} {b 4} {d 2} {e 1}
% lsort -index 0 {{a 5} { c 3} {b 4} {e 1} {d 2}}
{a 5} {b 4} { c 3} {d 2} {e 1}
% lsort -index 1 {{a 5} { c 3} {b 4} {e 1} {d 2}}
{e 1} {d 2} { c 3} {b 4} {a 5}

ソートによって重複を取り除きます

% lsort -unique {a b c a b c a b c}
a b c

比較関数を使うより複雑なソート

% proc compare {a b} {
    set a0 [lindex $a 0]
    set b0 [lindex $b 0]
    if {$a0 < $b0} {
        return -1
    } elseif {$a0 > $b0} {
        return 1
    }
    return [string compare [lindex $a 1] [lindex $b 1]]
}
% lsort -command compare \
        {{3 apple} {0x2 carrot} {1 dingo} {2 banana}}
{1 dingo} {2 banana} {0x2 carrot} {3 apple}

参照

list, lappend, lindex, linsert, llength, lsearch, lset, lrange, lreplace

キーワード

element, list, order, sort


Copyright © 1993 The Regents of the University of California. Copyright © 1994-1996 Sun Microsystems, Inc. Copyright © 1999 Scriptics Corporation Copyright © 2001 Kevin B. Kenny. All rights reserved. Copyright © 1995-1997 Roger E. Critchlow Jr.