From: johan556@... Date: 2006-12-21T17:19:39+09:00 Subject: Re: tricky sort for happy visitors of Paris On 12/20/06, Devin Mullins wrote: > > johan556@gmail.com wrote: > > It is of course very similar to the previously proposed solutions, but > > is more general in that it sorts strings with several numbers in them, > > treating each number "numerically" (one extreme example could be > > IP-numbers). > > What about this? > arr.sort_by {|s| s.scan(/\d+/).map {|n| n.to_i } } > My example with IP-numbers was perhaps an ill-chosen one. I just wanted to indicate that my solution could handle several numbers in the string. It also considers the string parts as significant. Suppose the input is like this: arr = [ "Paris 1", "Paris 5", "Paris 14", "Lyon 2", "Lyon 6", "Lyon 15", ] Your code will give: ["Paris 1", "Lyon 2", "Paris 5", "Lyon 6", "Paris 14", "Lyon 15"] and my code gives: ["Lyon 2", "Lyon 6", "Lyon 15", "Paris 1", "Paris 5", "Paris 14"] I guess the code one would choose depends on exactly what one wants to accomplish. In the general case it may be important to consider the string parts too (e.g. "Paris" and "Lyon") and not just look at the numbers. /johan