From: Daniel Waite Date: 2007-11-01T07:19:30+09:00 Subject: Sorting a string... I was porting a small Python script over to Ruby and realized Ruby does not sort strings as I expected it would. 'cba'.sort # ["cba"] So I wrote this... class String def sort bytes = Array.new self.each_byte { |byte| bytes << byte } bytes.sort.collect { |byte| byte.chr }.join end end 'cba'.sort # "abc" It's fairly clean, but I don't like the bytes = and self.each_byte bits. It feels like there should be a way to say something like... self.each_byte(&:collect).sort.collect { |byte| byte.chr }.join Or something like that. I think some of the frustration comes from Python seemingly being able to do this "out of the box." Unless, of course, the following Python code generates an array of single characters and not a single array with two strings (in which case Ruby CAN do the same): letters = list(state1 + state2) # I assume this is [ s1, s2 ] letters.sort() key = "".join(letters) Any ideas? -- Posted via http://www.ruby-forum.com/.