Hello,
I want to sort a hash by first the value and after that the key.
So I did this :
def solution(array, key)
array.sort_by{ |key,array[value]| key}
end
But now I see a syntaxt error
Roelof
Hello,
I want to sort a hash by first the value and after that the key.
So I did this :
def solution(array, key)
array.sort_by{ |key,array[value]| key}
end
But now I see a syntaxt error
Roelof
You cant use argument array[value] in list of enumerate variables.
What is a array variable?
Addition documentation:
28 2014 ., 15:01, Roelof W. [email protected] ():
You can?t use argument ?array[value]? in list of enumerate variables.What is a ?array? variable??
Addition documentation:?http://www.ruby-doc.org/core-2.1.1/Enumerable.html#method-i-sort_by
28 ??? 2014 ?., ? 15:01, Roelof W. <[email protected]> ???????(?):
For hash:
a = {:a => 6,:b =>5, :c =>4}
a.sort_by{|key,value| key} → [[:a, 6], [:b, 5], [:c, 4]]
a.sort_by{|key,value| value} → [[:c, 4], [:b, 5], [:a, 6]]
For array:
a = [6,2,7,9,4,2]
a.sort_by{|item| item} → [2, 2, 4, 6, 7, 9]
For array of hashes:
a = [{:a => 1, :b => 4},{:a => 2, :b => 3}]
a.sort_by{|i| i[:a]} → [{:a=>1, :b=>4}, {:a=>2, :b=>3}]
a.sort_by{|i| i[:b]} → [{:a=>2, :b=>3}, {:a=>1, :b=>4}]
28 2014 ., 15:42, Roelof W. [email protected] ():
Evgeniy schreef op 28-5-2014 13:26:
You can?t use argument ?array[value]? in list of enumerate variables.What is a ?array? variable??
Addition documentation:?http://www.ruby-doc.org/core-2.1.1/Enumerable.html#method-i-sort_by
28 ??? 2014 ?., ? 15:01, Roelof W. <[email protected]>???????(?):</div> <br> </div> </blockquote> <br> Array is hash which schould be sorted.<br> the key is the key which is sorted. <br> <br> Roelof<br> <br> </blockquote> <br> Found it but not complete right.<br> <br> ?array.sort_by{|key|array} <br> <br> But then the sort has the wrong order 2 is now before 1 <br> <br> Roelof<br> <br> <br>
Oh… now I see what you want.
def solution(array_of_hashes, key)
array_of_hashes.sort{|x,y| x[key] <=> y[key] }
end
One example:
hash = [{id: 2}, {id: 1}]
solution(a,:id) → [{id: 1}, {id: 2}]
Two example:
hash = [{:a=>1, :b=>4}, {:a=>2, :b=>3}]
solution(hash,:a) → [{:a=>1, :b=>4}, {:a=>2, :b=>3}]
solution(hash,:b) → [{:a=>2, :b=>3}, {:a=>1, :b=>4}]
28 2014 ., 16:09, Roelof W. [email protected] ():
Evgeniy wny not?
def solution(array_of_hashes, key)
array_of_hashes.sort_by{|h| h[key] }
end
sort_by{|key,value|
key}
On Wed, May 28, 2014 at 2:09 PM, Roelof W. [email protected] wrote:
def solution(array, key)
TypeError: no implicit conversion from nil to integerRoelof
You have an array of hashes. Each element of the array has a value by
which you want to sort. The method sort_by passes to the block each
element to compute a value by which to sort. So, the sort_by block
will receive each hash in turn. In the hash, you want to extract the
value for the :id key, to sort by that. So:
2.0.0p195 :001 > one, two = [{id: 1}, {id: 2}]
=> [{:id=>1}, {:id=>2}]
2.0.0p195 :002 > [one, two].sort_by {|h| h[:id]}
=> [{:id=>1}, {:id=>2}]
If you want to make it a method and specify a different key within the
hash to sort every time, you can do it like this:
2.0.0p195 :006 > def solution array_of_hashes, key
2.0.0p195 :007?> array_of_hashes.sort_by {|h| h[key]}
2.0.0p195 :008?> end
=> nil
2.0.0p195 :009 >
2.0.0p195 :010 > one, two = [{id: 1, weight: 10}, {id: 2, weight: 5}]
=> [{:id=>1, :weight=>10}, {:id=>2, :weight=>5}]
2.0.0p195 :011 > solution [one,two], :id
=> [{:id=>1, :weight=>10}, {:id=>2, :weight=>5}]
2.0.0p195 :012 > solution [one,two], :weight
=> [{:id=>2, :weight=>5}, {:id=>1, :weight=>10}]
Hope this helps,
Jesus.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs