From: dblack@... Date: 2006-12-21T10:39:24+09:00 Subject: Re: tricky sort for happy visitors of Paris Hi -- On Thu, 21 Dec 2006, William James wrote: > 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"] >> irb(main):003:0> % lizzy:~% >> >> -- >> Jos Backus >> jos at catnook.com > > Won't work for > > arrondissements = "Paris 1", "Paris 2","Paris 12", "Paris 3", > "Lyon 2", "Lyon 1" > > For the 2nd field, you need a numeric comparison. > > p arrondissements.sort_by {|e| e.split.inject{|a,b| [a,b.to_i] } } Or: require 'scanf' p @arrondissements.sort_by {|e| e.scanf("%s%d") } David -- Q. What's a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)