ruby sort_by 多个字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17076372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ruby sort_by multiple fields
提问by Mike
I'm running Ruby 1.9.3p392.
我正在运行 Ruby 1.9.3p392。
Item = Struct.new( :name, :dir, :sort_dir )
entries = ftp.list()
entries.map!{|e| Net::FTP::List.parse(e) }.map!{|e| Item.new( e.basename, e.dir?, (e.dir? ? 0 : 1) ) }
render json: entries.sort_by{ |e| [ e.sort_dir, e.name ]}
For some reason, I am not getting the results back as expected.
出于某种原因,我没有按预期获得结果。
I do get all folders first followed by all files, however, the name sorting is failing.
我确实先获取所有文件夹,然后是所有文件,但是,名称排序失败。
As an example, I get these for my folders:
例如,我为我的文件夹获取了这些:
- content
- images
- bin
- 内容
- 图片
- 垃圾桶
For files:
对于文件:
- global.asax
- web.config
- favicon.ico
- global.asax
- 网页配置
- 图标
It groups the dir/file portion correct, but the names are sorted incorrectly.
它将目录/文件部分正确分组,但名称排序不正确。
The output to the console looks like this after sorting:
排序后控制台的输出如下所示:
#<struct FtpController::Item name="Content", dir=true, sort_dir=0>
#<struct FtpController::Item name="Images", dir=true, sort_dir=0>
#<struct FtpController::Item name="Scripts", dir=true, sort_dir=0>
#<struct FtpController::Item name="Views", dir=true, sort_dir=0>
#<struct FtpController::Item name="bin", dir=true, sort_dir=0>
#<struct FtpController::Item name="Global.asax", dir=false, sort_dir=1>
#<struct FtpController::Item name="Web.config", dir=false, sort_dir=1>
#<struct FtpController::Item name="favicon.ico", dir=false, sort_dir=1>
#<struct FtpController::Item name="packages.config", dir=false, sort_dir=1>
#<struct FtpController::Item name="robots.txt", dir=false, sort_dir=1>
回答by Darshan Rivka Whittle
Your sorting works correctly in MRI Ruby 1.8.7, 1.9.3, and 2.0.0:
您的排序在 MRI Ruby 1.8.7、1.9.3 和 2.0.0 中正常工作:
Item = Struct.new(:name, :dir, :sort_dir)
entries = [Item.new('favicon.ico', false, 1), Item.new('bin', true, 0),
Item.new('web.config', false, 1), Item.new('images', true, 0),
Item.new('global.asax', false, 1), Item.new('content', true, 0)]
entries.sort_by{|e| [e.sort_dir, e.name]}
# => [#<struct Item name="bin", dir=true, sort_dir=0>,
# #<struct Item name="content", dir=true, sort_dir=0>,
# #<struct Item name="images", dir=true, sort_dir=0>,
# #<struct Item name="favicon.ico", dir=false, sort_dir=1>,
# #<struct Item name="global.asax", dir=false, sort_dir=1>,
# #<struct Item name="web.config", dir=false, sort_dir=1>]
Have you tried outputting the result of your sort_byto a console? I'm not familiar with the render json:portion of your code, but perhaps that's where things are going wrong. My best guess is that somehow in the conversion to JSON (if that's what it does) the sorting is getting messed up.
您是否尝试将结果输出sort_by到控制台?我不熟悉render json:您的代码部分,但也许这就是出错的地方。我最好的猜测是,在转换为 JSON 的过程中(如果这就是它所做的),排序会变得一团糟。
My other idea is that perhaps you expect sort_byto modify entries; it does not. If you want entriesitself to be sorted after the call, use sort_by!(note the !at the end of the method name).
我的另一个想法是,也许您希望sort_by修改entries; 它不是。如果您希望entries在调用后对自身进行排序,请使用sort_by!(注意!方法名称末尾的 )。
Update:It looks like the issue is that you want a case-insensitive search. Simply adding upcaseshould do the trick:
更新:看起来问题是您想要不区分大小写的搜索。简单地添加upcase应该可以解决问题:
entries.sort_by{|e| [e.sort_dir, e.name.upcase]}

