红宝石sort_by多个字段 [英] ruby sort_by multiple fields

查看:145
本文介绍了红宝石sort_by多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Ruby 1.9.3p392.

I'm running 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:

  1. 内容
  2. 图片
  3. bin

对于文件:

  1. global.asax
  2. web.config
  3. favicon.ico

它正确地将目录/文件部分分组,但是名称排序不正确.

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>

推荐答案

您的排序在MRI Ruby 1.8.7、1.9.3和2.0.0中正确运行:

Your sorting works correctly in MRI Ruby 1.8.7, 1.9.3, and 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>]

您是否尝试过将sort_by的结果输出到控制台?我对您的代码的render json:部分不熟悉,但这也许就是问题所在.我最好的猜测是,以某种方式转换为JSON(如果确实如此),排序就变得混乱了.

Have you tried outputting the result of your sort_by to 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修改entries;它不是.如果要在调用后对entries本身进行排序,请使用sort_by!(请在方法名称末尾注意!).

My other idea is that perhaps you expect sort_by to modify entries; it does not. If you want entries itself to be sorted after the call, use sort_by! (note the ! at the end of the method name).

更新:看起来问题在于您想要不区分大小写的搜索.只需添加upcase即可达到目的:

Update: It looks like the issue is that you want a case-insensitive search. Simply adding upcase should do the trick:

entries.sort_by{|e| [e.sort_dir, e.name.upcase]}

这篇关于红宝石sort_by多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆