Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4633

how to sort array in ruby if i have nil object inside array?

$
0
0

how to sort array if i have array like this in ruby?

example :

my_array = ["12 months", "13 months", nil, nil, "12"]

i want get result like this :

 my_array = ["12", "12 months", "13 months", nil, nil]

when i try :

my_array.sort{|x, y| x <=> y}

i get error like this :

ArgumentError Exception: comparison of String with nil failed

how to fix it?

thanks before


Viewing all articles
Browse latest Browse all 4633

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>