1

I have an array :

[26] pry(#<HomeController>)> repos.class
=> Array
[27] pry(#<HomeController>)> repos.size
=> 2319
[28] pry(#<HomeController>)> 

of Watchlists, which are composed of different types of elements :

[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">

how can I sort Watchlists elements using Watchlist field named watchers ?

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

2 Answers 2

2
repos.sort_by(&:watchers)

That returns the sorted array. To sort it in place, use sort_by! instead.

If it's possible that an element might not have a watchers method, you can try this instead:

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

If they're not all integers, you can convert them:

repos.sort_by { |e| e.watchers.to_i }
Sign up to request clarification or add additional context in comments.

3 Comments

that's what I did too, but I get back this : [31] pry(#<HomeController>)> repos.sort { |a, b| a.watchers <=> b.watchers } ArgumentError: comparison of Watchlist with Watchlist failed from /var/www/gitwatcher/vendor/bundle/ruby/1.9.1/bundler/gems/mongoid-be43b096e0a7/lib/mongoid/relations/proxy.rb:117:in `sort' [32] pry(#<HomeController>)>
Do all of the elements have watchers? And is it really an integer, or does it just look like one?
all elements have watchers, yes but they are not "clean" data type, sometimes they are integer, while sometimes they are strings ...
1

It looks like you want Array#sortby:

repos.sort_by{|elem| elem.watchers }

It'll sort the array by the value of the "watchers" field.

1 Comment

I get "ArgumentError: comparison of String with 461 failed" because some "elem.watchers" are of type "int" while others are "string". How can I convert it all in integers ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.