如何一次按多列对矩阵进行排序

时间:2019-06-21 16:21:45

标签: tcl

我使用包struct :: matrix并希望一次按两列或更多列对矩阵进行排序。有可能吗?

我阅读了struct :: matrix的手册页,但没有得到提示。

1 个答案:

答案 0 :(得分:1)

最简单的方法(因为底层的排序方法不能保证稳定)是计算额外的一列,该列包含将两个值组合在一起的复合归类键 ,然后对该列进行排序,最后再删除该列。

$m add column [lmap primary [$m get column 1] secondary [$m get column 2] {
    # There are many ways to make collation keys; here's one suitable for simple words...
    string cat $primary "," $secondary
}]
# Sort the rows by the final (new!) column
$m sort rows end
# Delete the no-longer-needed column
$m delete column end