문제

I have an array like this

 a=[["address", "US"], ["company", "apple"], ["CEO", ""], ["Website", ""]]

I need the first values like ["address", "company", "CEO","Website"] so what i did was

a.transpose

I got

[["address", "company", "CEO", "Website"], ["US", "apple", "", ""]]

Now I need to sort the first array.. and its corresponding value when I do

[["address", "company", "CEO", "Website"], ["US", "apple", "", ""]].sort

I get

[["US", "apple", "", ""], ["address", "company", "CEO", "Website"]]

But what I want is

 [["address", "CEO", "company", "Website"], ["US", "", "apple", "" ]

If any body could help that'd be gr8!

도움이 되었습니까?

해결책

You can sort it like that:

a.sort_by { |e| e[0].downcase } .transpose

This first sorts the array with your custom criteria, and after that transposes the result, as you need it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top