Ruby, 23 bytes
->a{a.sort_by{|i|~i%2}}
Explanation:
sort_by
sorts every number as if its value were the result of the block (~i%2
)
~x
is equivalent to -x-1
and takes precedence over %2
Odd numbers will evaluate to 0
and even numbers will evaluate to 1
so odd numbers will be sorted first.
###Barely related: this works on ruby from homebrew 2.5.1p57 (because it's based on a small bug) but only for non-negative integers, 20 bytes
Barely related: this works on ruby from homebrew 2.5.1p57 (because it's based on a small bug) but only for non-negative integers, 20 bytes
->a{a.sort{|i|i%-2}}
Explanation:
This uses sort
which expects a block that takes 2 values and returns -1
, 0
or 1
depending on whether the first one is bigger, they're equal or the second one is bigger.
The block given here ignores the second value and returns -1
if the first number is odd or 0
if it's even.
It's not guaranteed to work but it does in some (I think buggy) implementations.