开发者

Is there a way to use sort_by on a second criterion if the first is equal? [duplicate]

开发者 https://www.devze.com 2023-03-14 18:14 出处:网络
Th开发者_运维知识库is question already has answers here: Closed 11 years ago. Possible Duplicate:
Th开发者_运维知识库is question already has answers here: Closed 11 years ago.

Possible Duplicate:

Sort strings and numbers in Ruby

I have an array of place objects, each with a name (unique) and number (non-unique). Is there a simple way to use sort_by to first sort by number and then (within each number) by name?

I know I can write a custom block for sort, but if this is possible, it'd be even easier!


Not sure if this is what you mean by "custom block" but it seems pretty simple to me:

places.sort_by { |place| [place.number, place.name] }
0

精彩评论

暂无评论...
验证码 换一张
取 消