On 28.03.2009 05:04, Matthias Reitinger wrote:
> matt neuburg wrote:
>> If there are a lot of these, however, it would be much better to do a
>> keyed sort using sort_by. How to create keys depends on what you know
>> about the data. If we knew for a fact that the second half was always
>> between 1 and 99, it would be easy:
>>
>> arr = arr.sort_by do |x|
>>   x1, x2 = x.split(".")
>>   x1.to_i * 100 + x2.to_i
>> end
>>
>> If the second half can be any length, though, you'll have to determine
>> the maximum length first, and change that "100" multiplier accordingly.
> 
> Either that or use arrays as sort keys:
> 
>   arr = arr.sort_by do |x|
>     x.split(".").map {|i| i.to_i}
>   end

Here's a variant (1.9 only):

arr.sort_by {|x| x.scan(/\d+/).map(&:to_i)}

Cheers

	robert