-1

我有一个用例,其中一组文件被命名为:

`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"]
4

2 回答 2

3
files.sort_by{|f| f[/\d+/].to_i}
于 2013-02-05T11:57:23.797 回答
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
于 2013-02-05T11:35:48.090 回答