Skip to content

Instantly share code, notes, and snippets.

@georgyangelov
Created June 8, 2017 08:12
Show Gist options
  • Save georgyangelov/4b9e9d5cb28e1e8c61d9aa9014dd3412 to your computer and use it in GitHub Desktop.
Save georgyangelov/4b9e9d5cb28e1e8c61d9aa9014dd3412 to your computer and use it in GitHub Desktop.
Sorts the beginning of a sequence like another sequence
module Enumerable
def sort_like(template)
sort_by.with_index do |element, index|
key = block_given? ? yield(element) : element
[template.index(key) || template.size + index + 1, key]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment