0
My question is about sorting a hash within an array in Ruby.
I’m learning to code in Ruby, my first contact with programming, I’m exercising by consuming an api. I managed to get to where I wanted, which was to take the name of the clubs the position and print. My next goal is to sort clubs by position, but using (.Sort) always returns error (no implicit Conversion of Symbol into Integer)
the api I’m consuming is in this https://api.cartolafc.globo.com/clubes which basically returns a hash.
require 'rest-client'
require 'json'
url = 'https://api.cartolafc.globo.com/clubes'
resp = RestClient.get "#{url}"
clubes = JSON.parse(resp.body)
tabela = []
for chave, valor in clubes
nome = valor["nome"]
posicao = valor["posicao"]
if posicao != nil
tabela.push [:clube=>nome, :classificacao=>posicao]
puts "O nome do time é #{nome} e a posição é #{posicao}º"
end
end
res = tabela.sort_by { |item| item[:classificacao] }
puts res