Reputation: 63
I use the package struct::matrix and want to sort a matrix by two or more columns at once. Is it possible?
I read the man page of struct::matrix but I didn't get a hint.
Upvotes: 0
Views: 104
Reputation: 137587
The simplest method (because the underlying sort method is not guaranteed to be stable) is to compute an extra column that contains a compound collation key that combines the two values, then sort on that column, and finally delete the column afterwards.
$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
Upvotes: 1