Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

ruby sort_by twice

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...