Forum: Ruby Re: Array comparison returning nil

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-03-03 17:24
(Received via mailing list)
Could it be that some of the elements in your arrays cannot be
like "a" and 1 ?

You can help this by mapping all these elements to numbers or strings,
can be sorted,
as in this example

a = ["three",3, "four",4]
c = ["ten",10,"eleven",11]
b=a.sort{ |x,y|  x.to_s <=> y.to_s}<=>c.sort { |x,y| x.to_s <=> y.to_s}
puts  "result is #{b}"

which gives a result -1, since 3 is smaller than 10.

Best regards,

This topic is locked and can not be replied to.