Forum: Ruby Re: Array comparison returning nil

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
67bb4df2775f6a6b603347dce7119571?d=identicon&s=25 unknown (Guest)
on 2006-03-03 16:24
(Received via mailing list)
Could it be that some of the elements in your arrays cannot be
compared,
like "a" and 1 ?

You can help this by mapping all these elements to numbers or strings,
which
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,

Axel
This topic is locked and can not be replied to.