redim is for setting the dimension(size of the array) and you are doing that every row. Preserve just keeps the existing values.
with that said:
for m := 1 to ubound(u) do (
if u[m] <> 0 then (
p := p + 1;
redim preserve y;
y := u[m]
));
I would think would be like:
for m := 1 to ubound(u) do (
if u[m] <> 0 then (
p := p + 1;
redim preserve y
;
y := u[m]
));
redim preserve Final[ubound(y)];
//sorting
for p := 1 to ubound(y) do
...
as you only need to set the size of Final once
for sorting, wouldn't you want to arrange the values in Final in order? I don't see that happening in the code.
HTH
HTH