2012-11-30 103 views
1

我有一個數組:排序多維陣列中的Ruby

[26] pry(#<HomeController>)> repos.class 
=> Array 
[27] pry(#<HomeController>)> repos.size 
=> 2319 
[28] pry(#<HomeController>)> 
監視列表的

,其元素組成的不同的類型:

[29] pry(#<HomeController>)> repos[0] 
=> #<Watchlist _id: 4f82c1127c0c220001000002, _type: nil, searchable_values: ["description:grit", "description:gives", "description:you", "description:object", "description:oriented", "description:read", "description:write", "description:access", "description:to", "description:git", "description:repositories", "description:via", "description:ruby", "tag:git", "html_url:https", "html_url:github", "html_url:com", "html_url:mojombo", "html_url:grit"], arrangeable_values: {"description"=>"Grit gives you object oriented read/write access to Git repositories via Ruby.", "tag"=>"git", "html_url"=>"https://github.com/mojombo/grit"}, html_url: "https://github.com/mojombo/grit", description: "Grit gives you object oriented read/write access to Git repositories via Ruby.", forks: 269, watchers: 1536, created_at: 2007-10-29 14:37:16 UTC, pushed_at: 2012-09-04 21:54:09 UTC, avatar_url: "https://secure.gravatar.com/avatar/25c7c18223fb42a4c6ae1c8db6f50f9b?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", tags_array: ["git"], fork_: "false"> 

[30] pry(#<HomeController>)> repos[1] 
=> #<Watchlist _id: 4f82c1127c0c220001000003, _type: nil, searchable_values: ["description:ruby", "description:process", "description:monitor", "html_url:https", "html_url:github", "html_url:com", "html_url:mojombo", "html_url:god"], arrangeable_values: {"description"=>"Ruby process monitor", "tag"=>"", "html_url"=>"https://github.com/mojombo/god"}, html_url: "https://github.com/mojombo/god", description: "Ruby process monitor", forks: 218, watchers: 1181, created_at: 2008-01-13 05:16:23 UTC, pushed_at: 2012-10-02 23:15:44 UTC, avatar_url: "https://secure.gravatar.com/avatar/25c7c18223fb42a4c6ae1c8db6f50f9b?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", tags_array: [], fork_: "false"> 

我怎樣才能排序監視列表使用監視列表字段元素命名觀察者

[31] pry(#<HomeController>)> repos[0].watchers 
=> 1536 
[29] pry(#<HomeController>)> repos[1].watchers 
=> 1181 
[32] pry(#<HomeController>)> 

回答

2
repos.sort_by(&:watchers) 

即返回排序後的數組。要對其進行排序,請改用sort_by!

如果它可能是一個因素可能沒有watchers方法,你可以試試這個:如果他們不是所有整數

repos.sort_by { |e| e.watchers rescue 0 } 

,你可以將它們轉換:

repos.sort_by { |e| e.watchers.to_i } 
+0

這就是我所做的,但我回到這裏:[31] pry(#)> repos.sort {| a,b | a.watchers <=> b.watchers} 參數錯誤:自監視列表的監視列表比較失敗 源於/var/www/gitwatcher/vendor/bundle/ruby/1.9.1/bundler/gems/mongoid-be43b096e0a7/lib/mongoid/relations/ proxy.rb:117:'sort' [32] pry(#)> –

+1

所有的元素都有'watchers'嗎?它真的是一個整數,還是它看起來像一個? –

+0

所有元素都有觀察者,但他們不是「乾淨」的數據類型,有時他們是整數,有時他們是字符串... –

1

它看起來像你想Array#sortby

repos.sort_by{|elem| elem.watchers } 

它會排序數組由「觀察者」字段的值。

+0

我得到「ArgumentError:比較帶有461的字符串失敗「,因爲一些」elem.watchers「的類型是」int「,而另一些是」字符串「。我怎樣才能把它全部轉換成整數? –

+0

看到我的答案,盧卡。 –