Created
June 8, 2017 08:12
-
-
Save georgyangelov/4b9e9d5cb28e1e8c61d9aa9014dd3412 to your computer and use it in GitHub Desktop.
Sorts the beginning of a sequence like another sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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