irb> my_array.sort_by { |e| [ e == 'chicken' ? 0 : e == 'beef' ? 1 : 2, e ] }
This will create a sort key for each element of the array, and then sort the array elements by their sort keys. Since the sort key is an array, it is compared by position, so [0, 'chicken'] < [1, 'beef'] < [2, 'apple' ] < [2, 'banana'] .
If you donโt know which elements you wanted to sort to the front before execution, you can still use this trick:
irb> promotables = [ 'chicken', 'beef' ]
source share