I'd like to sort the first array:
filenames = ["z.pdf", "z.txt", "a.pdf", "z.rf", "a.rf","a.txt", "z.html", "a.html"]
by the following file's extensions array:
extensions = ["html", "txt", "pdf", "rf"]
using sort_by. But when I try:
filenames.sort_by { |x| extensions.index x.split('.')[1] }
I get:
["a.html", "z.html", "z.txt", "a.txt", "a.pdf", "z.pdf", "z.rf", "a.rf"]
The filenames with extensions "txt" and "rf" are not sorted. I've tried to figure out how sort_by sorts by using a tuple but haven't been able to find the source code for sort_by.
How can I sort one array by another array using sort_by?
Edit:
The result should look like:
["a.html", "z.html", "a.txt", "z.txt", "a.pdf", "z.pdf", "a.rf", "z.rf"]