2

I have some rather complex sorting to do.

Given an array of hashes, I need to sort the hashes within the array, based on a value in a key of each hash.

For example (given in JSON format):

"instruments":[
{"id":1,"title":"Piano","token":"piano","count":13},
{"id":6,"title":"Bass Guitar","token":"bass","count":12},
{"id":11,"title":"Viola","token":"viola","count":12},
{"id":4,"title":"Synth","token":"synth","count":11},
{"id":3,"title":"Keyboard","token":"keyboard","count":9},
{"id":7,"title":"Saxophone","token":"saxophone","count":8},
{"id":12,"title":"Flute","token":"flute","count":8},
{"id":5,"title":"Drums","token":"drums","count":6},
{"id":2,"title":"Guitar","token":"guitar","count":5},
{"id":8,"title":"Violin","token":"violin","count":5},
{"id":9,"title":"Vocals","token":"vocals","count":4},
{"id":10,"title":"Cello","token":"cello","count":4}
]

This JSON is the result of a responds_with in Rails. Before it gets to that, I'd like to switch these hashes around in the array, based on the alphabetical order of the "title" key in each hash.

Here's a screenshot of a part of the JSON (in Firebug) to better illustrate what I mean:

enter image description here

Any help would be greatly appreciated.

Thanks!

3 Answers 3

6

You're looking for Enumerable#sort_by.

instruments.sort_by { |x| x["title"] }
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, it's that easy! Brilliant.
1

You can use the ruby sort_by method (http://ruby-doc.org/core-2.2.0/Enumerable.html#method-i-sort_by)

So in your case where the instruments look like this:

instruments=[
{"id"=>1,"title"=>"Piano","token"=>"piano","count"=>13},
{"id"=>6,"title"=>"Bass Guitar","token"=>"bass","count"=>12},
{"id"=>11,"title"=>"Viola","token"=>"viola","count"=>12},
{"id"=>4,"title"=>"Synth","token"=>"synth","count"=>11},
{"id"=>3,"title"=>"Keyboard","token"=>"keyboard","count"=>9},
{"id"=>7,"title"=>"Saxophone","token"=>"saxophone","count"=>8},
{"id"=>12,"title"=>"Flute","token"=>"flute","count"=>8},
{"id"=>5,"title"=>"Drums","token"=>"drums","count"=>6},
{"id"=>2,"title"=>"Guitar","token"=>"guitar","count"=>5},
{"id"=>8,"title"=>"Violin","token"=>"violin","count"=>5},
{"id"=>9,"title"=>"Vocals","token"=>"vocals","count"=>4},
{"id"=>10,"title"=>"Cello","token"=>"cello","count"=>4}
]

You can use this to sort by the given column ("title" in your case)

 instruments.sort_by{|instrument| instrument["title"]}

Hope this Helps.

Comments

0

In case it helps anyone following on, my JSON is doubly-nested

products = [
{:product=>{:id=>866468155, :code=>"TB2", :name=>"Thunderbird 2"}},
{:product=>{:id=>805953484, :code=>"FAB1", :name=>"Rolls-Royce FAB 1"}},
{:product=>{:id=>715919491, :code=>"TB1", :name=>"Thunderbird 1"}},
{:product=>{:id=>767677594, :code=>"TB5", :name=>"Thunderbird 5"}},
]

And I was able to sort by name using,

table.sort_by! { |x| x[:product][:name]}

...note this also uses ! to sort in place, rather than creating a new Array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.