From: Kevin Smith Date: 2001-02-26T00:17:16+09:00 Subject: [ruby-talk:11535] Re: Sorting Guy N. Hurst wrote: >your code generates: >["Name 1", "Name 10", "Name 100", "Name 101", >"Name 11", "Name 2", "Name 200", "Name 22"] > >If I add "[1]to_i" after each split, then it does things better... Thanks for pointing this out. Of course you are correct. That's what I had intended to do, but I got so excited when the array <=> array just *worked* (before I looked it up in the book) that I forgot to continue coding until the task was done. (Now where did I put those unit tests again?) >b = strings.sort do | a, b | > a.split[1].to_i <=> b.split[1].to_i >end >Of course, this is assuming there is a second element in the array after the split, and >that it is convertible to an integer. Indeed. The original requirement was pretty vague. Prehaps the need was for each string to be sorted entirely, treating each word as a number where appropriate and text otherwise. In that case, I would write my own compare(array1, array2) method that did a split, and iterated through each word treating it appropriately. It may not be as clever as the regex based solutions, but I prefer clear, simple brute force unless there's some requirement that forces me in a different direction. Kevin