ruby sort_by 两次 [英] ruby sort_by twice

查看:38
本文介绍了ruby sort_by 两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby 在 Enumerables 上有一个 sort_by 方法.极好的!所以你可以做类似的事情

Ruby has a sort_by method on Enumerables. Fantastic! So you can do something like

entries.sort_by { |l| l.project.name }

这将按项目名称对一堆条目进行排序.您如何才能在具有相同名称的项目中按照时间对条目进行排序?

That would sort a bunch of entries by their project names. How could you work it so that within projects that had the same name, entries were sorted by their time?

推荐答案

我建议将要排序的列放入数组中.

I would suggest putting the column you want to sort by into an array.

entries.sort_by { |l| [l.project.name, l.project.time] }

这将尊重每种类型的自然排序顺序.

This will respect the natural sort order for each type.

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

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