Hello. I’m having a problem with saving of multidimensional array to CSV file, or just fetch.

The array has X and Y dimensions, which are the coordinates, and Z values.

When I save it to CSV file or just fetch, the X values are not consecutive, for example I get

{ X, Y}

0, 1

0, 2

0, 3

1, 1

1, 2

0, 4

0, 5

But I need it to be

{X, Y}

0, 1

0, 2

0, 3

0, 4

0, 5

1, 2

1, 2

How can I make it? Sorting by X first and after by Y does not help, because after Y sorting, X is not sorted anymore.

Any help will be highly appreciated.