How to sort a multidimensional ndslice?
Arredondo
arm.plus at gmail.com
Tue Aug 18 02:13:15 UTC 2020
I want to sort a two-dimensional ndslice by its columns according
to some predefined predicate.
What I mean is _not_ sorting the contents of each column
individually, but instead to reorder the entire columns of the
matrix so that they are sorted according to some "greater than"
function.
Here's a MWE (the `larger` function is just an example):
```
import std.stdio;
import mir.ndslice.slice;
import mir.ndslice.sorting;
void main() {
auto a = [[1, -1, 3, 2],
[0, -2, 3, 1]].sliced;
writeln(a);
a.byDim!1.sort!((u, v) => larger(u, v));
writeln(a);
}
auto larger(C)(C u, C v) {
import mir.math.sum : sum;
return sum(u) > sum(v);
}
```
I would have expected this to print:
[[1, -1, 3, 2], [0, -2, 3, 1]]
[[3, 2, 1, -1], [3, 1, 0, -2]]
but instead it prints
[[1, -1, 3, 2], [0, -2, 3, 1]]
[[1, -1, 3, 2], [0, -2, 3, 1]]
i.e, nothing happens!
Any suggestions?
Arredondo.
More information about the Digitalmars-d-learn
mailing list