Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby / Active Record: custom sorting order

I'm working on a rails 3.2 ruby 1.9.2 project.

I get some values from database with a classic:

designators = Model.find()

And I show it in view with (simplified code):

<table class="rwy_modes_table" cellspacing='0'>
  <% designators.each do |info_design| %>
    <tr id="rwy_mode_<%=info_design.id%>">
      <td class="spacer"><%= info_design.try(:designator) %></td>
    </tr>
  <% end %>
</table>

Values are for example: 3L, 3C, 3R. (L for Left, C for Center, 4 for Right). I would like values ordered such as: 3L, 3C, 3R and not 3C, 3L, 3R

I don't know how to define this custom order. Any idea ?

like image 449
franchez Avatar asked Aug 25 '14 10:08

franchez


1 Answers

Try something like this:

(app/models/designator.rb)
# determines the sort value for based on my_attribute.
# designators should be in the order 'l', 'c', 'r' - regardless of any preceeding numeral
def sort_val
  return 0 if self.my_attribute =~ /l/i
  return 1 if self.my_attribute =~ /c/i
  return 2 if self.my_attribute =~ /r/i
end

(app/helpers/designators_helper.rb)
def sorted_designators(designators)
  designators.sort{|a, b| a.sort_val <=> b.sort_val }
end

Where you can do sorting in your view

(app/views/...)
sorted_designators(designators).each do |design|

Alternately, you can move this to a static method in your model file, for easy sorting outside your view

(app/models/designator.rb)
def self.sorted_designators(designators)
  designators.sort{|a, b| a.sort_val <=> b.sort_val }
end

and use them in your controller like this

app/controllers...
@designators = Designator.sorted_designators(designators)

Note: this is an in-memory sort, so watch for O(n) db queries depending on how you sort / access this object

See also: https://stackoverflow.com/a/14282353/811255 for using the Comparable module (probably a cleaner solution)

like image 152
MaximusDominus Avatar answered Nov 25 '22 06:11

MaximusDominus