Tcl/Tk命令详解之列表处理函数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子列表,将会根据子列表来进行排序,举例如下:
lsort -integer -index 1 / - {{First 24} {Second 18} {Third 30}}
返回 {Second 18} {First 24} {Third 30}, and
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} {a 1 e i} {b 2 3 f g}, and
lsort -index {0 1} { - {{b i g} 12345}
- {{d e m o} 34512}
- {{c o d e} 54321}
- }
- 返回 {{d e m o} 34512} {{b i g} 12345} {{c o d e} 54321} (因为e排列在i前,i排列在o前。)这个选项在这种方式的比较中比-command更有效。
- -nocase
- 进行排序的时候不考虑大小写,如果与-dictionary、-integer、或-real命令合并使用则没有作用。
- -unique
- 如果这个选项指定了,如果遇到相同的比较元素,那么只保留最后的那个,其它的全部删除掉。因此如果-index 0使用了,那么{1 a}和{1 b}比较时就是相同的,所以只有{1 b}返回。
注意:
lsort命令只用来控制使用什么样的排序关系,并不改变数值本身,lsort命令是安全的当用于-command选项时。
例:
使用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}
剥离重复的数据:
% lsort -unique {a b c a b c a b c}
a b c
返回排序后元素在原序列的索引:
% lsort -indices {c a b}
1 2 0
更加复杂的排序方式:
% 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}