Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 6th, 2008, 08:12 AM
ab12cd34 ab12cd34 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Location: Melbourne
Posts: 11 ab12cd34 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 1 m 33 sec
Reputation Power: 0
Question Sorting

Hi,

Does anyone have the idea of how 'sort' function is implemented in the class 'Array'?


Thanks!!!

Reply With Quote
  #2  
Old March 6th, 2008, 11:07 AM
h4z3 h4z3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 41 h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level)h4z3 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 17 h 50 m 13 sec
Reputation Power: 41
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
Comments on this post
Joseph Taylor agrees!

Reply With Quote
  #3  
Old March 7th, 2008, 10:44 AM
ab12cd34 ab12cd34 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Location: Melbourne
Posts: 11 ab12cd34 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 1 m 33 sec
Reputation Power: 0
Smile

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Sorting

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap