Created
July 24, 2015 18:43
-
-
Save adilsoncarvalho/f09a055f831c86026625 to your computer and use it in GitHub Desktop.
Ordenando valores usando um dicionário de apoio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
elementos = %w(sub_grupo loja setor grupo fornecedor) | |
# => ["sub_grupo", "loja", "setor", "grupo", "fornecedor"] | |
elementos.sort | |
# => ["fornecedor", "grupo", "loja", "setor", "sub_grupo"] | |
indice = { loja: 0, fornecedor: 1, setor: 2, grupo: 3, sub_grupo: 4 } | |
# => {:loja=>0, :fornecedor=>1, :setor=>2, :grupo=>3, :sub_grupo=>4} | |
elementos.sort{|a,b| indice[a.to_sym] <=> indice[b.to_sym] } | |
# => ["loja", "fornecedor", "setor", "grupo", "sub_grupo"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment