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/.

Daniel Waite wrote:

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?

class String
  def sort
    self.split('').sort.join
  end
end

'cba'.sort #=> "abc"

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Thu, 1 Nov 2007, Daniel Waite wrote:

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"

I wouldn't overwrite a core method like that; you could end up with
some very unexpected results. It's better to give it a different name.

David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
   * Advancing With Rails, Edison, NJ, November 6-9
   * Advancing With Rails, Berlin, Germany, November 19-22
   * Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!

Daniel Waite wrote:

letters = list(state1 + state2) # I assume this is [ s1, s2 ]

No. '+' is used for string concatenation in python just as it is in
ruby:

···

state1 = "xxx"
state2 = "AAA"
letters = list(state1 + state2)

output:--
['x', 'x', 'x', 'A', 'A', 'A']

--
Posted via http://www.ruby-forum.com/\.

Daniel Waite wrote:

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...

If you just want to sort by byte-value (i.e. disregarding the
possibility of a multibyte-encoding and ignoring umlauts etc.), you can
do:
str.unpack("C*").sort.pack("C*")
It's about twice as fast as
str.split(//).sort.join

Regards
Stefan

···

--
Posted via http://www.ruby-forum.com/\.

David Mullet wrote:

class String
  def sort
    self.split('').sort.join
  end
end

'cba'.sort #=> "abc"

Brilliant! Thanks, David. I love this community. :slight_smile:

I had tried split, but in haste, with no arguments. However, after
reading the docs again I see no arguments equals a space.

Interesting how the documentation has a perfect example:

"hello".split(//) #=> ["h", "e", "l", "l", "o"]

When writing that extension I kept thinking, all I need is an array, how
can I get one? Now I know!

···

--
Posted via http://www.ruby-forum.com/\.

David A. Black wrote:

Hi --

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"

I wouldn't overwrite a core method like that; you could end up with
some very unexpected results. It's better to give it a different name.

Also, note that String#sort depends on String#to_a being defined, which is no longer true in 1.9. It's kind of an accident that "cba".sort works at all.

I guess that means someone will be free to implement String#sort, as long as they stick to 1.9. Maybe there will be a core implementation that works as expected?

···

On Thu, 1 Nov 2007, Daniel Waite wrote:

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

# splort for split sort :wink:
def splort s
  s.split('').sort!.join('')
end

···

On Oct 31, 7:26 pm, "David A. Black" <dbl...@rubypal.com> wrote:

On Thu, 1 Nov 2007, Daniel Waite wrote:
> class String
> def sort
> end
> end

I wouldn't overwrite a core method like that; you could end up with
some very unexpected results. It's better to give it a different name.

Quoth Joel VanderWerf:

David A. Black wrote:
> Hi --
>
>
>> 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"
>
> I wouldn't overwrite a core method like that; you could end up with
> some very unexpected results. It's better to give it a different name.

Also, note that String#sort depends on String#to_a being defined, which
is no longer true in 1.9. It's kind of an accident that "cba".sort works
at all.

I guess that means someone will be free to implement String#sort, as
long as they stick to 1.9. Maybe there will be a core implementation
that works as expected?

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Well, anyone who's sorting a string really just wants to sort the component
letters, so my_str.split('').sort.join('') is the way to go. If they want to
add a method to String to simplify this, they can do that.

···

> On Thu, 1 Nov 2007, Daniel Waite wrote:

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/

The sort! is unnecessary, use sort (no exclamation)

···

On 10/31/07, Brian Adkins <lojicdotcom@gmail.com> wrote:

# splort for split sort :wink:
def splort s
  s.split('').sort!.join('')
end

-------------------------------------------
Daniel Brumbaugh Keeney
Devi Web Development
Devi.WebMaster@gMail.com
-------------------------------------------

Presuming an English alphabetic order, perhaps yes.
What about case-sensitivity?
What about other languages? Ordering always depends on little details. A basic English alphabetic ordering might be an overly simple use-case. Although, it is a great example of a Ruby one-liner.
Remember unicode support is coming (we hope) to Ruby 2, and 1.9 is really an unfinished 2.

···

On Oct 31, 2007, at 6:46 PM, Konrad Meyer wrote:

Quoth Joel VanderWerf:

David A. Black wrote:

Hi --

On Thu, 1 Nov 2007, Daniel Waite wrote:

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"

I wouldn't overwrite a core method like that; you could end up with
some very unexpected results. It's better to give it a different name.

Also, note that String#sort depends on String#to_a being defined, which
is no longer true in 1.9. It's kind of an accident that "cba".sort works
at all.

I guess that means someone will be free to implement String#sort, as
long as they stick to 1.9. Maybe there will be a core implementation
that works as expected?

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Well, anyone who's sorting a string really just wants to sort the component
letters, so my_str.split('').sort.join('') is the way to go. If they want to
add a method to String to simplify this, they can do that.

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/

Based on what criterion/criteria?

If there is a desire not to unnecessarily allocate an extra array, the
exclamation point is necessary.

You could say that the split() is unnecessary, because you can just do
this instead:
  def splort s
    a =
    s.each_byte{ |b| a << b.chr }
    a.sort.join('')
  end

···

On Nov 1, 2:00 pm, devi.webmas...@gmail.com wrote:

On 10/31/07, Brian Adkins <lojicdot...@gmail.com> wrote:

> # splort for split sort :wink:
> def splort s
> s.split('').sort!.join('')
> end

The sort! is unnecessary, use sort (no exclamation)

Just out of curiosity, what is the reason you prefer sort over sort!
in this scenario?

···

On Nov 1, 4:00 pm, devi.webmas...@gmail.com wrote:

On 10/31/07, Brian Adkins <lojicdot...@gmail.com> wrote:

> # splort for split sort :wink:
> def splort s
> s.split('').sort!.join('')
> end

The sort! is unnecessary, use sort (no exclamation)

> The sort! is unnecessary, use sort (no exclamation)

Based on what criterion/criteria?

I suppose I wasn't thinking. For some reason sort would be a cheaper
operation than sort!, but sort involves object instantiation, which is
much more expensive. My apologies.

···

-------------------------------------------
Daniel Brumbaugh Keeney
Devi Web Development
Devi.WebMaster@gMail.com
-------------------------------------------

They're actually not that different in this case. I also thought sort!
would be much quicker since it sorts in place, but it doesn't seem to
be when I benchmarked both of them. I just continued using sort! on
principle :slight_smile:

···

On Nov 1, 4:23 pm, devi.webmas...@gmail.com wrote:

> > The sort! is unnecessary, use sort (no exclamation)

> Based on what criterion/criteria?

I suppose I wasn't thinking. For some reason sort would be a cheaper
operation than sort!, but sort involves object instantiation, which is
much more expensive. My apologies.