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)
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
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?
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.
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.
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
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.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.