Is there a way of changing the key of a hash via sort / sort_by?

ruby sort array of hashes by key
ruby hash sort by key
ruby hash#sort_by
ruby hash sort by value
rails sort hash by value
sort hash by key perl
ruby sort hash by value descending
ruby sort hash by multiple values

Currently working through a question thats trying to get me to transform the former into the later

{ a: 2, b: 5, c: 1 } => { a: 1, b: 2, c: 5 }

tried to do this with

hash = { a: 2, b: 5, c: 1 }.sort_by {|k,v| v}.to_h

this gives this => {:c=>1, :a=>2, :b=>5}

how do I change the key of the hash whilst sorting the values?

Looks like you're trying to split the hash into keys and values, sort each of those separately, and then put them back together as a hash.

In that case, you could do something like this:

Step by step it works like this:

# First array-ify the hash into key/value pairs
# [[:a, 2], [:b, 5], [:c, 1]] 

# Then transpose to group the keys and values together
# [[:a, :b, :c], [2, 5, 1]]

# Then sort the keys and values separately
# [[:a, :b, :c], [1, 2, 5]] 

# And transpose again to get key/value pairs
# [[:a, 1], [:b, 2], [:c, 5]] 

# And combine the array of key/value pairs into a hash
# {:a=>1, :b=>2, :c=>5} 

You could also manually do the hash.to_a.transpose step like this:

[hash.keys, hash.values].map(&:sort).transpose.to_h

You don't even have to assume that #keys and #values will produce arrays in any particular order since you're sorting everything anyway.

How to sort a Ruby Hash alphabetically by keys, Assuming you want the output to be a hash which will iterate through keys in sorted order, then you are nearly there. Hash#sort_by returns an� Before getting into this article, you if just need to sort a Perl hash by the hash key, this is a pretty well-known recipe. It's covered in another Q&A article titled "How to sort a hash by the hash key". Sorting a Perl hash by the hash value. Sorting a Perl hash by the hash value is a bit more difficult than sorting by the key, but it's not

What's about the following approach?

{ a: 2, b: 5, c: 1 }.then { |hash| [hash.keys.sort, hash.values.sort].transpose }.to_h

Sorting a Hash by Key or Value Code Example, Get code examples like "Sorting a Hash by Key or Value" instantly right from your google Hashes are unsorted objects because of the way in which they are stored. 13 You can do the same by value, but it's a little more complicated: 23. . 24. my_hash.keys.sort_by { |key| my_hash[key] }.each do |key|. My Hashtable has these months as keys and there is value which is associated with each of these keys that is a bonus.These key - value pair has to be displayed in a table in above said sorting order. I will make an assumption here that my Hashtable is populated with correct values.

h = { a: 2, b: 5, c: 1 }

a = h.values.sort
  #=> [1, 2, 5] 
h.transform_values { a.shift }
  #=> {:a=>1, :b=>2, :c=>5} 

See Hash#transform_values.

Ruby Hash#sort_by. Ruby#sort and #sort_by methods are…, Surprisingly, there are not many examples in Ruby document library for So what if we want to sort by hash values instead of the key's order? Luckily, Hash has the Enumerable module mixed in which provides us with methods like sort and sort_by. Let's give them a go: people.sort # NoMethodError: undefined method ` =>' for :joan:Symbol The sort method uses the <=> comparison operator to put things into order.
#  {:a=>1, :b=>2, :c=>5}

This just sorts the values, not the keys. Since your original example had pre-sorted keys, it wasn't clear from the question whether you expected those to be sorted as well if they weren't already

If you want to sort the keys and the values and have them linked to each other, then you can sort the keys as well:
# {:a=>1, :b=>2, :c=>5}

sort (Hash), Converts hsh to a nested array of [ key, value ] arrays and sorts it, using Array# sort. h = { "a" => 20 To sort a hash with symbol keys, use Enumerable#sort_by: If we would like to make sure the result of sort is more predictable, we will need to sort based on two (or even more) values. For example we can sort the hash first by the Position value, and among the entries with the same Position value we can sort by the value of the Max field. In order to do this we will use the following expression:

Hash#sort � GitHub, ##Hash#keys and Hash#values A simple way to sort a hash keys or values is to use the methods Hash#keys and Hash#values. Those 2 methods Well, we can use Hash#sort_by So because Ruby is lazy, it doesn't bother changing back the array into a hash. It is, because you have so much power through that block ! I want to sort them based on those. I tried calling sort_by or sort on keys, but since they are stored in string format, it sorts them alphabetically. hash_name.keys.sort This gives me in order of Aug 2012, July 2011, Oct 2014. While I'm trying to sort them in order of the year and month as --- July 2011, Aug 2012, Oct 2014

Getting error sorting the hash, That is, frequencies.sort_by {|k,v| v } does not change the value of frequencies at all! You're sorting and totally discarding the result. This is the cause of the� How do I sort a hash (optionally by value instead of key)? (contributed by brian d foy) To sort a hash, start with the keys. In this example, we give the list of keys to the sort function which then compares them ASCIIbetically (which might be affected by your locale settings).

How to sort a Ruby Hash by number value?, The problem I am running into is that the default Hash.sort function sorts numbers Ruby Hash#sort_by - Florence Liang, Therefore, if we want to return the Sorting a Hash by Key or Value, Hashes are unsorted objects because of the way in It is similar to an Array, except that indexing is done via arbitrary keys of any �

  • @lurker Ruby hashes are ordered these days, they maintain insertion order just like JavaScript objects (in recent versions of JavaScript of course).
  • @muistooshort ok thanks, I wasn't aware of that.