in reply to move all 0s in an array to the beginning keeping other elements order same
sort:
Note that this is not a stable sort for the zeroes. For that you could use@array = sort {-($a == 0) || $b == 0} @array;
or even@array = sort {($b == 0)-($a == 0)} @array;
Of course, I'd curse the programmer that used this in production code.@array = sort {!$b - !$a} @array;
#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 |