Matt Huggins Matt Huggins - 1 month ago 6
Ruby Question

Sort specific items of an array first

I have a ruby array that looks something like this:

my_array = ['mushroom', 'beef', 'fish', 'chicken', 'tofu', 'lamb']


I want to sort the array so that 'chicken' and 'beef' are the first two items, then the remaining items are sorted alphabetically. How would I go about doing this?

Answer
irb> my_array.sort_by { |e| [ e == 'chicken' ? 0 : e == 'beef' ? 1 : 2, e ] }
 #=> ["chicken", "beef", "fish", "lamb", "mushroom", "tofu"]

This will create a sorting key for each element of the array, and then sort the array elements by their sorting keys. Since the sorting key is an array, it compares by position, so [0, 'chicken'] < [1, 'beef'] < [2, 'apple' ] < [2, 'banana'].

If you don't know what elements you wanted sorted to the front until runtime, you can still use this trick:

 irb> promotables = [ 'chicken', 'beef' ]
  #=> [ 'chicken', 'beef' ]
 irb> my_array.sort_by { |e| [ promotables.index(e) || promotables.size, e ] }
  #=> ["chicken", "beef", "fish", "lamb", "mushroom", "tofu"]
 irb> promotables = [ 'tofu', 'mushroom' ]
  #=> [ 'tofu', 'mushroom' ]
 irb> my_array.sort_by { |e| [ promotables.index(e) || promotables.size, e ] }
  #=> [ "tofu", "mushroom", "beef", "chicken", "fish", "lamb"]