I'm trying to sort in descending order an array of photo objects from Flickr API based on the number of comments(count_comments) of each photo. I'm using the following code.
def rank_photos(photos)
photos.sort_by { |photo| photo.count_comments * -1 }
end
However I get the following error message.
*': negative argument (ArgumentError)
Here is what the Array looks like
[{"id"=>"38280904752", "owner"=>"131718287@N07", "secret"=>"abe0b93180", "server"=>"4583", "farm"=>5, "title"=>"IMG_3640", "ispublic"=>1, "isfriend"=>0, "isfamily"=>0, "count_comments"=>"0", "tags"=>"washington post dc web women codeher17 dctech tech technology", "url_m"=>"https://farm5.staticflickr.com/4583/38280904752_abe0b93180.jpg", "height_m"=>"333", "width_m"=>"500"}, {"id"=>"38312540901", "owner"=>"131718287@N07", "secret"=>"7b6e6805d4", "server"=>"4568", "farm"=>5, "title"=>"IMG_3458", "ispublic"=>1, "isfriend"=>0, "isfamily"=>0, "count_comments"=>"0", "tags"=>"washington post dc web women codeher17 dctech tech technology", "url_m"=>"https://farm5.staticflickr.com/4568/38312540901_7b6e6805d4.jpg", "height_m"=>"500", "width_m"=>"333"}, {"id"=>"38281453252", "owner"=>"131718287@N07", "secret"=>"438293cffd", "server"=>"4539", "farm"=>5, "title"=>"IMG_3460", "ispublic"=>1, "isfriend"=>0, "isfamily"=>0, "count_comments"=>"0", "tags"=>"washington post dc web women codeher17 dctech tech technology", "url_m"=>"https://farm5.staticflickr.com/4539/38281453252_438293cffd.jpg", "height_m"=>"333", "width_m"=>"500"}
Why is throwing this error?
photo.count_comments.to_i * -1. The error message is saying that the methodString#*requires a non-negative argument. For example,"cat" * 3 #=> "catcatcat". A negative argument (instead of3) would make no sense here. - Cary Swoveland"count_comments"=>"0"socount_commentsis a string. Seems a bit odd but there you go. - mu is too short