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

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-forum.com/.

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-forum.com/\.

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-forum.com/\.

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

···

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!

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