I have a following array
我有一个以下数组
arr = ["2014-05-02T19-49-55_1280x720_vga2usb.mp4", "2014-05-02T19-49-55_544x288_left_cam.mp4", "2014-05-02T19-49-55_544x288_right_cam.mp4", "2014-05-02T19-49-55_1632x288.mp4"]
I need to sort this array in a way that I get following:
我需要按照以下方式对此数组进行排序:
["2014-05-02T19-49-55_1632x288.mp4", "2014-05-02T19-49-55_544x288_left_cam.mp4", "2014-05-02T19-49-55_544x288_right_cam.mp4", "2014-05-02T19-49-55_1280x720_vga2usb.mp4"]
I tried to use the sort method arr.sort
and arr.sort_by{|word| word.downcase}
But it is giving me output as
我尝试使用sort方法arr.sort和arr.sort_by {| word | word.downcase}但它给我的输出为
["2014-05-02T19-49-55_1280x720_vga2usb.mp4", "2014-05-02T19-49-55_1632x288.mp4", "2014-05-02T19-49-55_544x288_left_cam.mp4", "2014-05-02T19-49-55_544x288_right_cam.mp4"]
How can I do this?
我怎样才能做到这一点?
1 个解决方案
#1
0
Try to use sort like this:
尝试使用这样的排序:
arr.sort { |x,y| y <=> x }
If you need to sort by substring try something like this:
如果你需要按子串排序尝试这样的事情:
arr.sort { |x,y| y[m,n] <=> x[m,n]}
EDIT: this should work:
编辑:这应该工作:
arr.sort do |x,y|
regex = /x[[:digit:]]+(.+?)$/
regex.match(x)[1] <=> regex.match(y)[1]
end
#1
0
Try to use sort like this:
尝试使用这样的排序:
arr.sort { |x,y| y <=> x }
If you need to sort by substring try something like this:
如果你需要按子串排序尝试这样的事情:
arr.sort { |x,y| y[m,n] <=> x[m,n]}
EDIT: this should work:
编辑:这应该工作:
arr.sort do |x,y|
regex = /x[[:digit:]]+(.+?)$/
regex.match(x)[1] <=> regex.match(y)[1]
end