in reply to move all 0s in an array to the beginning keeping other elements order same

sort:

@array = sort {-($a == 0) || $b == 0} @array;
Note that this is not a stable sort for the zeroes. For that you could use
@array = sort {($b == 0)-($a == 0)} @array;
or even
@array = sort {!$b - !$a} @array;
Of course, I'd curse the programmer that used this in production code.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: move all 0s in an array to the beginning keeping other elements order same
by ikegami (Patriarch) on May 02, 2014 at 17:05 UTC
    Add use sort 'stable'; to ensure you get the correct result.