我想对第一个数组进行排序:
filenames = ["z.pdf", "z.txt", "a.pdf", "z.rf", "a.rf","a.txt", "z.html", "a.html"]
通过以下文件的扩展名数组:
extensions = ["html", "txt", "pdf", "rf"]
使用sort_by
. 但是当我尝试时:
filenames.sort_by { |x| extensions.index x.split('.')[1] }
我得到:
["a.html", "z.html", "z.txt", "a.txt", "a.pdf", "z.pdf", "z.rf", "a.rf"]
扩展名为“txt”和“rf”的文件名没有排序。我试图弄清楚如何sort_by
使用元组进行排序,但无法找到sort_by
.
如何使用另一个数组对一个数组进行排序sort_by
?
编辑:
结果应如下所示:
["a.html", "z.html", "a.txt", "z.txt", "a.pdf", "z.pdf", "a.rf", "z.rf"]