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.
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!
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
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
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.
The sort! is unnecessary, use sort (no exclamation)
···
On 10/31/07, Brian Adkins <lojicdotcom@gmail.com> wrote:
# splort for split sort
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.
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.
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
> 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
···
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.