Skip to main content
Commonmark migration
Source Link

Ruby, 23 bytes

->a{a.sort_by{|i|~i%2}}

Try it online!

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.

Ruby, 23 bytes

->a{a.sort_by{|i|~i%2}}

Try it online!

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

->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.

Ruby, 23 bytes

->a{a.sort_by{|i|~i%2}}

Try it online!

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

->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.

added 670 characters in body
Source Link
Asone Tuhid
  • 2.4k
  • 8
  • 22

Ruby, 23 bytes

->a{a.sort_by{|i|~i%2}}

Try it online!

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

->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.

Ruby, 23 bytes

->a{a.sort_by{|i|~i%2}}

Try it online!

###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}}

Ruby, 23 bytes

->a{a.sort_by{|i|~i%2}}

Try it online!

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

->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.

added 4 characters in body
Source Link
Asone Tuhid
  • 2.4k
  • 8
  • 22

Ruby, 23 bytes

->a{a.sort_by{|i|~i%2}}

Try it online!

###Barely related: this works on ruby from homebrew 2.5.1p57 (because it's based on a small bug) but only for positivenon-negative integers, 20 bytes

->a{a.sort{|i|i%-2}}

Ruby, 23 bytes

->a{a.sort_by{|i|~i%2}}

Try it online!

###Barely related: this works on ruby from homebrew 2.5.1p57 (because it's based on a small bug) but only for positive integers, 20 bytes

->a{a.sort{|i|i%-2}}

Ruby, 23 bytes

->a{a.sort_by{|i|~i%2}}

Try it online!

###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}}
deleted 137 characters in body
Source Link
Asone Tuhid
  • 2.4k
  • 8
  • 22
Loading
added 134 characters in body
Source Link
Asone Tuhid
  • 2.4k
  • 8
  • 22
Loading
Source Link
Asone Tuhid
  • 2.4k
  • 8
  • 22
Loading