· ruby

Ruby: Sorting by boolean fields

We were doing a bit of work on RapidFTR in the ThoughtWorks Pune office today and one problem my pair and I were trying to solve was how to sort a collection of objects by a boolean field.

Therefore given the following array of values:

form_sections = [FormSection.new(:enabled => false, :name => "a", :order => 1), 
                 FormSection.new(:enabled => true, :name => "b", :order => 2)]

We wanted to display those form sections which were disabled at the bottom of the page.

We originally tried the following:

form_sections.sort_by { |row| [row.enabled, row.order] }

But got the following error:

undefined method `<=>' for true:TrueClass

We figured we’d need to convert the true and false values to equivalent values which is demonstrated on this Stack Overflow post:

form_sections.sort_by { |row| [row.enabled ? 0 : 1, row.order] }

I didn’t realise it would be that simple to do - it’s pretty neat.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket