From: Duane Johnson Date: 2006-12-21T07:59:00+09:00 Subject: Re: tricky sort for happy visitors of Paris ------=_Part_35337_27132970.1166655535893 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 12/20/06, Jos Backus wrote: > > More generally: > > lizzy:~% irb > irb(main):001:0> arrondissements = ["Paris 1", "Paris 2","Paris 4", "Paris > 3", "Lyon 2", "Lyon 1"] > => ["Paris 1", "Paris 2", "Paris 4", "Paris 3", "Lyon 2", "Lyon 1"] > irb(main):002:0> sorted_array = arrondissements.sort_by {|e| [e.split(" > ")]} > => ["Lyon 1", "Lyon 2", "Paris 1", "Paris 2", "Paris 3", "Paris 4"] > I think that gets us back to the original problem, namely, that numbers with an unequal number of digits will be incorrectly sorted using due to string comparison: irb(main)> arrondissements = ["Paris 1", "Paris 2","Paris 4", "Paris 3", "Lyon 2", "Lyon 1", "Paris 10"] irb(main)> arrondissements.sort_by {|e| [e.split(" ")]} => ["Lyon 1", "Lyon 2", "Paris 1", "Paris 10", "Paris 2", "Paris 3", "Paris 4"] Note the position of "Paris 10". This is why the zero-padding is necessary in my previous example. -- Duane Johnson (canadaduane) ------=_Part_35337_27132970.1166655535893--