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

**URL:** <https://www.ruby-forum.com/t/order-notes-starting-at-c-thru-b-rather-than-a-thru-g/157005>\
**Category:** Ruby\
**Created:** [January 6, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/order-notes-starting-at-c-thru-b-rather-than-a-thru-g/157005 "2009-01-06T12:05:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 6, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/order-notes-starting-at-c-thru-b-rather-than-a-thru-g/157005/1 "2009-01-06T12:05:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 6, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/order-notes-starting-at-c-thru-b-rather-than-a-thru-g/157005/2 "2009-01-06T12:15:18Z")

</div>

Max W. 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  
\<=\>

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 6, 2009, 12:17pm UTC](https://www.ruby-forum.com/t/order-notes-starting-at-c-thru-b-rather-than-a-thru-g/157005/3 "2009-01-06T12:17:56Z")

</div>

Brian C. 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!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 6, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/order-notes-starting-at-c-thru-b-rather-than-a-thru-g/157005/4 "2009-01-06T13:42:27Z")

</div>

On 06.01.2009 12:17, Max W. wrote:

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