Search

Chaps

Sweet sweet memories

Blather

Uptime verified by Wormly.com

31 May 2008

Sort by two object attributes in Ruby

This is a potentially ugly to solve, but you can actually compare two arrays. So it isn't so bad. If the first attributes are the same, it sorts by the second.

def <=>(other)
  [self.dtstart, self.dtend] <=> [other.dtstart, other.dtend]
end

Comments

  1. Hi Ryan,
    I cannot understand this at all. Nor can I understand why I can’t get to my footboot email with my usual password any more. What do I have to do?

    Stella / 10:26am / 10 June 2008

  2. Hi Stella. It should be working again now. I had changed the address because I’d been told I should, but as it turned out that was the wrong thing to do.

    Ryan / 10:43am / 10 June 2008

  3. Thanks Ryan. Problem solved!

    Stella / 10:16am / 11 June 2008

Leave a comment

Markdown
  • **bold text**
  • *italicized text*
  • > This text is blockquoted
  • * This is an unordered list item
  • 1. This is an ordered list item
  • [Link Text](http://www.example.com/)
  • `code item`

0.277 seconds