开发者

sorting 2d array with nil value

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 co开发者_如何学运维uld 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜