From: Daniel Finnie Date: 2008-03-03T08:03:09+09:00 Subject: Re: Building your own string sorting method A basic string sorting routine would just sort based on ASCII codepoints (see a list at http://urltea.com/2tu7 ). So you could do this in Ruby 1.9: irb(main):002:0> class String irb(main):003:1> def <=> other irb(main):004:2> self.each_char.map(&:ord) <=> other.each_char.map(&:ord) irb(main):005:2> end irb(main):006:1> end <, >, ==, !=, etc. are all defined in terms of <=>, the spaceship operator, so all of the previous operations are affected when we define <=>. Arrays sort by comparing the first non-matching element. irb(main):011:0> %w[a c b g h j g p q].sort => ["a", "b", "c", "g", "g", "h", "j", "p", "q"] Of course this has a lot of deficiencies: irb(main):014:0> %w[a c b g h j g p q Z A G].sort => ["A", "G", "Z", "a", "b", "c", "g", "g", "h", "j", "p", "q"] So we could downcase each character or something. But even advanced sorting algorithms have problems -- German sorts beta as nn or something and lots of other languages have similar issues. Book listings generally don't include words like "the" when sorting, etc. I would decide those before starting. On Sun, Mar 2, 2008 at 5:50 PM, Joseph L. Casale wrote: > I am trying to find examples of this to help me but can't seem to locate any on the web. Anyone know of any online examples? I know ruby has a built in method for sorting an array, but it's part of a tutorial... > > Thanks! > jlc >