From: matt@... (matt neuburg) Date: 2009-03-29T01:40:03+09:00 Subject: Re: Sorting decimal index numbers? 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 Yes, that's better; my first answer failed to take into account Array's existing implementation of the spaceship operator. m. -- matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/ Leopard - http://www.takecontrolbooks.com/leopard-customizing.html AppleScript - http://www.amazon.com/gp/product/0596102119 Read TidBITS! It's free and smart. http://www.tidbits.com