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?
使用sort_by
:
Names.sort_by {|name| name[1]}
#=> ["AJackson", "RNixon", "BObama", "GWashington"]
我认为您只需要使用枚举器方法sort_by
并传入适当的块即可。像:
Names.sort_by{|name| name[1]}