Sorting - Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)

Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)

Sorting

Algorithms and the processes for sorting are a large intellectual chunk of the computer science world. In fact, it can be studied to almost endless capacity. However, Ruby will save us time with its already built in sorting methods.

ary = [2,5,4,1,3]

ary.sort!

print ary

[1, 2, 3, 4, 5]

We can even use .sort! to sort alphabetically.

ary = ["c","b","d","e","a"]

ary.sort!

print ary

["a", "b", "c", "d", "e"]

Recall the Spaceship Operator:

3 <=> 4 returns -1

3 <=> 3 returns 0

4 <=> 3 returns 1

We can use the Spaceship Operator on strings.

"boots" <=> "cats" returns -1

What kind of Quantum Mechanics is this? Relax Schrodinger. I like where you're going with this though. Let's keep the theme alive:

"cat" <=> "box" returns 1

Because "c" in cat is higher up the alphabet, it is considered greater than the "b" in box. If we assigned corresponding numbers to letters when comparing "c" to "b" is would look like this:

c <=> b returns 1

3 <=> 2 returns 1

When both the first letters in our strings are the same Ruby compares the second letter and so on:

"camp" <=> "cat" returns -1

Capital letters in Ruby come before their lowercase counterparts.

"Capital" <=> "capital" returns -1

If you're having trouble bridging the gap between numbers and letters, just replace the phrase "less than" with "sooner than". Mapped as: (ABC…Z abc...z) Ah ha! It makes perfect sense now.

Create an array of people and use the sort! method on them.

ary = ["Sarah", "Tom", "Mike", "Sue", "Frank", "Kim"]

print ary.sort!

["Frank", "Kim", "Mike", "Sarah", "Sue", "Tom"]

Now add the reverse! method to sort in descending order.

ary = ["Sarah", "Tom", "Mike", "Sue", "Frank", "Kim"]

print ary.sort!.reverse!

["Tom", "Sue", "Sarah", "Mike", "Kim", "Frank"]

We're going to sort the more complicated way by using a block and iteration. You know, because it's fun.

ary = ["Sarah", "Tom", "Mike", "Sue", "Frank", "Kim"]

ary.sort! {|name1, name2| name2 <=> name1}

print ary

["Tom", "Sue", "Sarah", "Mike", "Kim", "Frank"]

The cat's out of the box now. What is happening here? We are taking the first item and comparing it to the second. Then the second to the third. We continue this process throughout the array. To help, imagine we're going to sort these in alphabetical order.

"Sarah" <=> "Tom" returns -1

At this point Ruby knows that Sarah must be sooner than Tom.

"Tom" <=> "Mike" returns 1

Tom comes after Mike. We still need to check if Mike comes before Sarah though.

"Mike" <=> "Sarah" returns -1

Indeed it does. So far Ruby knows the alphabetical ascending order is this: Mike, Sarah, Tom. Ruby will continue checking all the items and going through this process until each is in its proper place.

This will give us ascending order. We needed to reverse it though. We were able to reverse it by switching the arguments around in our block.

{|name1, name2| name2 <=> name1}

By doing this we switched the results of our comparisons around.

This concept can be a bit tricky to wrap your head around. I recommend watching the related video if you're needing help with it.