[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Order notes starting at C thru B rather than A thru G

Max Williams

1/6/2009 11:06:00 AM

Say i have a bunch of note objects, which have a 'name' string, eg 'A'
'B#', 'Cb', 'D' etc.

I want to order them starting at 'C' through to 'B' - what's a simple
way to do this?

thanks
max
--
Posted via http://www.ruby-....

7 Answers

Brian Candler

1/6/2009 11:15:00 AM

0

Max Williams wrote:
> Say i have a bunch of note objects, which have a 'name' string, eg 'A'
> 'B#', 'Cb', 'D' etc.
>
> I want to order them starting at 'C' through to 'B' - what's a simple
> way to do this?

Define a <=> operator in your class.

class Note
include Comparable
def <=>(other)
name <=> other.name # modify this to give your desired ordering
end
end

'include Comparable' also gives you <, >, <= and >= operators based on
<=>
--
Posted via http://www.ruby-....

Max Williams

1/6/2009 11:18:00 AM

0

Brian Candler wrote:

>
> Define a <=> operator in your class.
>
> class Note
> include Comparable
> def <=>(other)
> name <=> other.name # modify this to give your desired ordering
> end
> end
>
> 'include Comparable' also gives you <, >, <= and >= operators based on
> <=>

thanks!
--
Posted via http://www.ruby-....

Robert Klemme

1/6/2009 11:38:00 AM

0

On 06.01.2009 12:17, Max Williams wrote:
> Brian Candler wrote:
>
>> Define a <=> operator in your class.
>>
>> class Note
>> include Comparable
>> def <=>(other)
>> name <=> other.name # modify this to give your desired ordering
>> end
>> end
>>
>> 'include Comparable' also gives you <, >, <= and >= operators based on
>> <=>
>
> thanks!

An alternative approach is to do

notes.sort_by {|note| ... appropriate extraction here... }

or

notes.sort {|note_a, note_b| ... desired ordering code ... }

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end

Alphonsus Jr.

1/12/2013 4:27:00 AM

0

On Friday, January 11, 2013 7:31:17 PM UTC-8, Fadosolrélamisi wrote:
> On Friday, January 11, 2013 6:32:39 PM UTC-8, Alphonsus Jr. wrote:
>
> > So little gratitude. Very disappointing. :-(
>
>
>
> Indeed ... but you ought to know that your threads are always stammeringly taking of and that they usualy end up in the freezing cold high end of the upper guitar neck when everyone ends up talking like they have inhale helium gazes! Now picture that ... and please ... no thanks!

Huh???

Alain Reiher

1/12/2013 5:50:00 AM

0

On Friday, January 11, 2013 8:26:47 PM UTC-8, Alphonsus Jr. wrote:
> On Friday, January 11, 2013 7:31:17 PM UTC-8, Fadosolrélamisi wrote:
>
> > On Friday, January 11, 2013 6:32:39 PM UTC-8, Alphonsus Jr. wrote:
>
> >
>
> > > So little gratitude. Very disappointing. :-(
>
> >
>
> >
>
> >
>
> > Indeed ... but you ought to know that your threads are always stammeringly taking of and that they usualy end up in the freezing cold high end of the upper guitar neck when everyone ends up talking like they have inhale helium gazes! Now picture that ... and please ... no thanks!
>
>
>
> Huh???

You see! Now begins the stammer ... how many rmcguitaers will participate .... that's always an imponderable. may you be praised enough not to escape vanity.
Thanks.

Richard Yates

1/12/2013 1:38:00 PM

0

On Fri, 11 Jan 2013 21:50:04 -0800 (PST), Fadosolrélamisi
<reiher@telus.net> wrote:

>May you be praised enough not to escape vanity.

Best post of the week.

Alphonsus Jr.

1/12/2013 7:01:00 PM

0

On Saturday, January 12, 2013 5:37:47 AM UTC-8, Richard Yates wrote:
> On Fri, 11 Jan 2013 21:50:04 -0800 (PST), Fadosolrélamisi
>
> <reiher@telus.net> wrote:
>
>
>
> >May you be praised enough not to escape vanity.
>
>
>
> Best post of the week.

Agreed! I enjoyed that one a lot.