From: Nobuyoshi Nakada Date: 2009-03-28T14:30:23+09:00 Subject: Re: Sorting decimal index numbers? Hi, At Sat, 28 Mar 2009 13:04:43 +0900, Matthias Reitinger wrote in [ruby-talk:332318]: > > 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 enc/depend file in 1.9 has more generic code. alphanumeric_order = proc {|e| e.scan(/(\d+)|(\D+)/).map {|n,a| a||[n.size,n.to_i]}.flatten} But this fails if strings start with digit and strings start with non-digit are mixed. In such case, this is safer. alphanumeric_order = proc {|e| e.scan(/(\A\D*|\G\D+)(\d+)/).map{|a,n|[a,n.size,n.to_i]}.flatten} -- Nobu Nakada