From: Aaron Vegh Date: 2009-03-28T11:57:53+09:00 Subject: Re: Sorting decimal index numbers? Hi Matt, Are you _the_ Matt Neuburg, of AppleScript fame? It's a privilege to have an answer from you! matt neuburg wrote: > These are not numbers; they're strings. Or at least, they need to be; if > they weren't, 1.1 and 1.10 would be indistinguishable. Yes, I think this was becoming my assumption. The head-scratcher for me was trying to determine if this sort of numbering scheme ("the hierarchical list", if you will) was a special case in Ruby or generally. But in my model right now (this is for a Rails project) it's set as a string. > 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 And it turns out that a hundred is more than enough in this case. Thanks to you, and to Yossef above, for a working solution. Cheers! Aaron. -- Posted via http://www.ruby-forum.com/.