TCL自學筆記-17 lsort命令

名稱

lsort - 給列表中的元素排序

語法

lsort ?options? list

描述

這個命令對list中的元素進行排序,返回一個重新排序的列表。lsort命令的執(zhí)行使用歸并排序算法,所以排序性能穩(wěn)定在(n log n)的級別。

默認使用ASCII表排序,但是以下的options選項也可以控制排序的方式:

-ascii

使用 ASCII表的字符順序排序,默認方式。

-dictionary

使用字典關系,基本上和 -ascii差不多,區(qū)別在于 -dictionary忽略了大小寫關系,而且把數字字符看作是數值來處理。比如在 -dictionary模式,bigBoy在bigbang和bigboy中間,x10y在x9y和x11y之間。

-integer

把列表元素轉換成整數并且使用整數關系排序。

-real

把列表元素轉換成浮點數并且使用浮點數關系排序。

-command

 

command

使用 command作為一個比較命令,比較兩個元素。這個命令需要返回一個整數,表示小于、等于或大于0,分別對應第一個元素是小于、等于還是大于第二個元素。

-increasing

排序時按照由小到大的順序排列,默認方式。

-decreasing

排序時按照由大到小的順序排列。

-indices

返回一個列表,包含的元素是排序后的元素對應以前的 list的索引。

-index

 

indexList

如果指定了這個選項,那么 list的元素必須是一個合法的Tcl子列表,將會根據子列表來進行排序

示例

使用ASCII對列表排序:

% lsort {a10 B2 b1 a1 a2}

B2 a1 a10 a2 b1

使用字典關系對列表排序:

% 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}

運行程序,以下real用法注意:

(Documents) 65 % lsort -integer {5 3 1 2 11 4}

1 2 3 4 5 11

(Documents) 66 % lsort real {5 3 1 2 11 4}

bad option "real": must be -ascii, -command, -decreasing, -dictionary, -increasing, -index, -indices, -integer, -nocase, -real, or -unique

(Documents) 67 % 

堅持每天學習,時間會證明一切,加油!

登錄后免費查看全文
立即登錄
App下載
技術鄰APP
工程師必備
  • 項目客服
  • 培訓客服
  • 平臺客服

TOP

3