TCL自学笔记-17 lsort命令

名称

lsort - 给列表中的元素排序

语法

lsort ?options? list

描述

这个命令对list中的元素进行排序,返回一个重新排序的列表。lsort命令的执行使用归并排序算法,所以排序性能稳定在(n log n)的级别。

默认使用ASCII表排序,但是以下的options选项也可以控制排序的方式:

-ascii

使用 ASCII表的字符顺序排序,默认方式。

-dictionary

使用字典关系,基本上和 -ascii差不多,区别在于 -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