我有一个用例,其中一组文件命名为:
`a1@1x.jpg, a2@1x.jpg, a3@1x.jpg....a10@1x.jpg, a11@1x,jpg,a12@1x.jpg.... a99@1x.jpg,a100@1x.jpg`
当我在数组中读到这个时,我得到的订单为(files=Dir.entries("./pics").grep(/^#{fileNamePattern}[0-9]/))
["a1@1x.jpg", "a10@1x.jpg", "a11@1x.jpg".... "a2@1x.jpg", "a20@1x.jpg"...] and so on
我想根据“ a ”之后的数字按升序对此数组进行排序
["a1@1x.jpg", "a2@1x.jpg", "a3@1x.jpg"...."a10@1x.jpg", "a11@1x.jpg","a12@1x.jpg"...."99@1x.jpg","a100@1x.jpg"]
答案 0 :(得分:3)
files.sort_by{|f| f[/\d+/].to_i}
答案 1 :(得分:0)
尝试
files=Dir.entries("./pics").grep(/^#{fileNamePattern}[0-9]/)
files = files.sort_by do |f|
f =~ /^{#fileNamePattern}(\d*)/ # Match filename, extracting number
$1.to_i # Convert extracted number-string to integer
end