The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Ruby Programming
|
Sorting
Discuss Sorting in the Ruby Programming forum on Dev Shed. Sorting Ruby and Ruby on Rails programming forum covering Ruby Tips and Tricks, Best Practices, and agile development with Ruby on Rails.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 6th, 2008, 08:12 AM
|
|
Registered User
|
|
Join Date: Feb 2008
Location: Melbourne
Posts: 11
Time spent in forums: 2 h 1 m 33 sec
Reputation Power: 0
|
|
Sorting
Hi,
Does anyone have the idea of how 'sort' function is implemented in the class 'Array'?
Thanks!!!
|

March 6th, 2008, 11:07 AM
|
|
|
|
I know Ruby code isn't exactly fully documented, but its always worth a look.. Here is what the Array#sort docs say:
Returns a new array created by sorting self. Comparisons for the sort will be done using the <=> operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1. See also Enumerable#sort_by.
a = [ "d", "a", "e", "c", "b" ]
a.sort #=> ["a", "b", "c", "d", "e"]
a.sort {|x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
Unless there was more to your question, as the docs say, sort uses the <=> operator or a block. Not sure if this answers your question or not. If not you may have to elaborate
|

March 7th, 2008, 10:44 AM
|
|
Registered User
|
|
Join Date: Feb 2008
Location: Melbourne
Posts: 11
Time spent in forums: 2 h 1 m 33 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by h4z3 I know Ruby code isn't exactly fully documented, but its always worth a look.. Here is what the Array#sort docs say:
Returns a new array created by sorting self. Comparisons for the sort will be done using the <=> operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1. See also Enumerable#sort_by.
a = [ "d", "a", "e", "c", "b" ]
a.sort #=> ["a", "b", "c", "d", "e"]
a.sort {|x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
Unless there was more to your question, as the docs say, sort uses the <=> operator or a block. Not sure if this answers your question or not. If not you may have to elaborate |
Thank you !!!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|