1

I have an array of names and I need to sort the array based off of the second letter of each name.

So if I have a list Names = [ "GWashington", "AJackson", "RNixon", "BObama" ]

How would I sort it so the array is organised by last name?

4

2 回答 2

5

使用sort_by

Names.sort_by {|name| name[1]}
#=> ["AJackson", "RNixon", "BObama", "GWashington"]
于 2013-09-22T03:37:02.933 回答
2

我认为您只需要使用枚举器方法sort_by并传入适当的块即可。像:

Names.sort_by{|name| name[1]}
于 2013-09-22T03:37:18.723 回答