The following may be alternative... defining minimum matrix size of min. one row and min. two columns:

```
z = Matrix(Max(1, MatrixRows), Max(min_col_size=2, MatrixColumns), 0);
rownum = MxGetSize(z, 0);
colnum = MxGetSize(z, 1);
last_column = colnum-1;
last_bi = colnum-min_col_size;// last idx of 1-dim array
```

And if you want to store the actual **visible** chart then you may do like so (instead of using BarCount -> See QuickAFL -> Extra safety bars added -> initial BackwardRef set to 30).

```
bi = BarIndex();
fvb = FirstVisibleValue(bi);
lvb = LastVisibleValue(bi);
barnum = lvb-fvb+1;
selectedColIndex = SelectedValue(bi) - fvb;
```

So,

```
/// @link https://forum.amibroker.com/t/question-regarding-sorting-arrays/10235/6
/// @link https://forum.amibroker.com/t/question-regarding-sorting-arrays/10235/9
//********************************** SORTING ***************************
// I can add any number of array comma separated here
columnnames = "Open,Close";
bi = BarIndex();
fvb = FirstVisibleValue(bi);
lvb = LastVisibleValue(bi);
barnum = lvb-fvb+1;
selectedColIndex = SelectedValue(bi) - fvb;
printf("visible barnum: %g, BarCount: %g\n", barnum, BarCount);
MatrixRows = StrCount(columnnames, ",")+1;
MatrixColumns = barnum + 1;// 1 extra column for storing row idx
z = Matrix(Max(1, MatrixRows), Max(2, MatrixColumns), 0);
rownum = MxGetSize(z, 0);
colnum = MxGetSize(z, 1);
last_column = colnum-1;
last_bi = last_column-1;// last idx of 1-dim array
for (row=0;row < rownum; row++) {
//Assign the arrays to each row..
arr = VarGet(StrExtract(columnnames, row));
z = MxSetBlock(z, row, row, 0, last_bi, arr);
// this last column is used to store counter like a unique row Id,
//so if sorted i can reach out to correct column when displaying
z[row][last_column] = row;
}
"\nBefore sorting";
MxToString(z);
//finally do a descending order sort
z = mxSortRows( z, False, selectedColIndex );
"After sorting";
MxToString(z);
//************************************** END SORTING ****************************
for (row = 0; row < rownum; row++) {
printf( "%s:%g (column:%g)\n", StrExtract(columnnames, z[row][last_column]), z[row][selectedColIndex], selectedColIndex+1);
}
```