How to sort 2D Slice along 0 axis in mir.ndslice ?

p.shkadzko p.shkadzko at gmail.com
Tue Mar 10 23:31:55 UTC 2020


I need to reproduce numpy sort for 2D array.

----------------------------------
import numpy as np

a = [[1, -1, 3, 2], [0, -2, 3, 1]]
b = np.sort(a)
b
# array([[-1,  1,  2,  3],
#        [-2,  0,  1,  3]])
----------------------------------

Numpy sorted the array by columns, which visually looks like each 
row of elements was sorted individually.
Going through 
http://docs.algorithm.dlang.io/latest/mir_ndslice_sorting.html I 
couldn't find analogous operation.

So, attempting to do the same in mir.ndslice results in the 
following:

-----------------------------------------------
import mir.ndslice;

auto m = [1, -1, 3, 2, 0, -2, 3, 1].sliced(4, 2);
// [[1, -1], [3, 2], [0, -2], [3, 1]]

m.sort;
// [[-2, -1], [0, 1], [1, 2], [3, 3]]
-----------------------------------------------

It basically flattened the 2D slice, sorted and reshaped it back 
into 2D with elements being moved.
Trying to do something like m.map!(a => a.sort); won't work 
because "a" is an int "1" and not a slice of two ints "[1, -1]". 
You can do it with foreach loop but then you'll have to allocate 
new elements. How do you do it in-place with mir?


More information about the Digitalmars-d-learn mailing list