Hourglass Contracts & Milestone Invoicing

Hourglass is a web-based invoicing application that makes sure you get paid, on time and every time.

Collect deposits, deliver milestone invoices and start every project with one of our contract templates

5 Things Highly-Paid Rails Consultants Do Differently

With this valuable course, you'll learn the secrets of how to:

  • Regain valuable hours back by hacking your workflow
  • Build a one-man consultancy and work wherever you want
  • Attract high-paying clients you'll love working with, every time
  • Build passive income by productizing what you're already selling!
P.S.: We'll send you actionable advice. No junk. Unsubscribe anytime.

Free Guide!

Five Things Highly-Paid Freelancers Do Differently

Using Ruby: How To Sort A Hash

If you're new to Ruby or programming in general, interacting with hashes may have you a little confused.

A hash is made up of keys and values (you can think of it like a dictionary with words and definitions). If we were to create a hash of dogs and their ages, it may look something like this:

dogs = {
  :sam => 6,
  :jerry => 8,
  :red => 4,
  :sandy => 1
}

In this example, we have four dogs (which are the "keys") and their ages (which are the "values").

There's a bunch of different ways that we can sort this hash. If we want to sort the all of the key and value pairs by their keys, we can just call sort on it.

# calling sort will sort keys and values by keys
dogs.sort
=> [[:jerry, 8], [:red, 4], [:sam, 6], [:sandy, 1]

Did you notice that what we got back was an array (it's actually an array of arrays)? Any time you call sort on a hash, you'll be getting back an array.

But what if we're only looking to get the keys from this hash? For that, we can isolate the keys by calling keys on the hash first and then sort.

# calling keys.sort will sort keys only
dogs.keys.sort
 => [:jerry, :red, :sam, :sandy]

Pretty cool! As you might be able to guess, sort by just the values is achieved by calling values first and then sort.

# calling values.sort will sort values only
dogs.values.sort
=> [1, 4, 6, 8]

Advanced Hash Sorting

But what happens if you're trying to deal with complex hashes? Well, let's see:

advanced_dogs = {
  :sam => { :name => "Sam", :age => 6, :speed=> 49 },
  :jerry => { :name => "Jerry", :age => 8, :speed => 56 },
  :red => { :name => "Red", :age => 4, :speed => 77 },
  :sandy => { :name => "Sandy", :age => 1, :speed => 39 }
}

We can still call sort on this hash and it will return a sorted array. But if we're trying to sort by the nested attributes, we'll need to use sort_by and specify the key we're interested in.

# let's sort our (advanced) dogs by speed
advanced_dogs.sort_by { |k,v| v[:speed] }
=> [[:sandy, {:name=>"Sandy", :age=>1, :speed=>39}],
    [:sam, {:name=>"Sam", :age=>6, :speed=>49}],
    [:jerry, {:name=>"Jerry", :age=>8, :speed=>56}],
    [:red, {:name=>"Red", :age=>4, :speed=>77}]
    ]

Sweet! Now we have a list of our dogs sorted by speed. Did you notice what happened in that code block with |k,v| v[:speed]? We specified that we wanted to sort by values of :speed key. The same could have been done for the :age key.

Hope that this helps you on your Ruby hash sorting journey!

5 Things Highly-Paid Rails Consultants Do Differently

With this valuable course, you'll learn the secrets of how to:

  • Regain valuable hours back by hacking your workflow
  • Build a one-man consultancy and work wherever you want
  • Attract high-paying clients you'll love working with, every time
  • Build passive income by productizing what you're already selling!
P.S.: We'll send you actionable advice. No junk. Unsubscribe anytime.

Free Guide!

Five Things Highly-Paid Freelancers Do Differently