On Mar 27, 2012, at 00:59 , Ri Houjun wrote:

> i have this array
> 
> b=[a1 a3 a2 a4 a7 a6 a5 a8 a9 a10]
> 
> why is it when I perform b.sort!
> the result is:
> b=[a1 a10 a2 a3 a4 a5 a6 a7 a8 a9]
> 
> how can i make it
> b=[a1 a2 a3 a4 a5 a6 a7 a8 a9 a10]

p Dir["f*.txt"].sort
# => ["f0.txt", "f1.txt", "f10.txt", "f11.txt", ..., "f2.txt", "f3.txt", ...]

class Array
  def human_sort
    sort_by { |item| item.to_s.split(/(\d+)/).map { |e| [e.to_i, e] } }
  end
end

p Dir["f*.txt"].human_sort
# => ["f0.txt", "f1.txt", "f2.txt", "f3.txt", "f4.txt", "f5.txt", "f6.txt", ...]
```