The following code just sorts each row:

------
/+dub.sdl:
dependency "mir-algorithm" version="~>3.9.24"
+/
import mir.ndslice;
import mir.ndslice.sorting;
import mir.algorithm.iteration: each;

void main() {
        // fuse, not sliced if you use an array of arrays for argument
    auto a = [[1, -1, 3, 2],
              [0, -2, 3, 1]].fuse;

    // make an index
        a.byDim!0.each!(sort!larger);

        import std.stdio;
    writeln(a);
  // [[3, 2, 1, -1], [3, 1, 0, -2]]
}

auto larger(C)(C u, C v) {
    return u > v;
}
------



To reorder the columns data according to precomputed index:

------
/+dub.sdl:
dependency "mir-algorithm" version="~>3.9.24"
+/
import mir.ndslice;
import mir.series;
import mir.ndslice.sorting;
import mir.algorithm.iteration: each;
import mir.math.sum;

void main() {
        // fuse, not sliced if you use an array of arrays for argument
    auto a = [[1, -1, 3, 2],
              [0, -2, 3, 1]].fuse;

    // make an index
        auto index = a.byDim!1.map!sum.slice;

    auto s = index.series(a.transposed);
    auto indexBuffer = uninitSlice!int(s.length);
    auto dataBuffer = uninitSlice!int(s.length);
    sort!larger(s, indexBuffer, dataBuffer);

    import std.stdio;
    writeln(a);
   /// [[3, 2, 1, -1], [3, 1, 0, -2]]
}

auto larger(C)(C u, C v) {
    return u > v;
}

-----

Reply via email to