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?

Daniel W. 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”

David M. 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!

Hi –

On Thu, 1 Nov 2007, Daniel W. wrote:

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

David A. Black wrote:

‘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?

Quoth Joel VanderWerf:

So I wrote this…

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.

Daniel W. 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’]

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

‘cba’.sort # [“cba”]

is no longer true in 1.9. It’s kind of an accident that “cba”.sort
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 M. [email protected] http://konrad.sobertillnoon.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.

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

On 10/31/07, Brian A. [email protected] wrote:

splort for split sort :wink:

def splort s
s.split(‘’).sort!.join(‘’)
end

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

On Nov 1, 4:00 pm, [email protected] wrote:

On 10/31/07, Brian A. [email protected] 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 Oct 31, 7:26 pm, “David A. Black” [email protected] wrote:

On Thu, 1 Nov 2007, Daniel W. 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.

splort for split sort :wink:

def splort s
s.split(‘’).sort!.join(‘’)
end

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.

On Nov 1, 2:00 pm, [email protected] wrote:

On 10/31/07, Brian A. [email protected] wrote:

splort for split sort :wink:

def splort s
s.split(‘’).sort!.join(‘’)
end

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

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, 4:23 pm, [email protected] 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.

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: