如何一次按多列对矩阵进行排序
How to sort a matrix by more than one column at once
我使用包 struct::matrix 并希望同时按两列或多列对矩阵进行排序。可能吗?
我阅读了 struct::matrix 的手册页,但没有得到任何提示。
最简单的方法(因为底层排序方法不保证稳定)是计算一个包含复合排序规则键 合并两个值,然后对该列进行排序,最后删除该列。
$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
我使用包 struct::matrix 并希望同时按两列或多列对矩阵进行排序。可能吗?
我阅读了 struct::matrix 的手册页,但没有得到任何提示。
最简单的方法(因为底层排序方法不保证稳定)是计算一个包含复合排序规则键 合并两个值,然后对该列进行排序,最后删除该列。
$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