Computer Science Canada Easily Sorting Complex Data |
Author: | wtd [ Fri Jun 24, 2005 7:13 pm ] | ||||||||||||||||||||||||
Post subject: | Easily Sorting Complex Data | ||||||||||||||||||||||||
Let's say we have a bunch of data, like an array of names. Let's further specify that each of those names is a hash, like:
Now, let's programmatically build a list of, oh... 10 names.
Now we have 10 names in an array. Let's just quickly look at a bit of block usage to clean this up. Blocks are very important and powerful in Ruby.
Now, how do we sort this based on first or last name? Well, let's look at the hard way first. Let's start with sorting by last name. First we need to extract a list of all of the last names.
We also need to sort that.
We also want to remove duplicates, which we might as well do before sorting.
Now, we have a list of sorted last names, and only unique last names. Let's print the names in this sorted order. We need to iterate over the sorted last names. For each sorted last name we need to iterate over the original set of name data and only print a name if the last names match.
Now, if we want to put all of the names in sorted order into a new array, we do something very similar.
We can use the select method to clean up that inner loop.
Or... We could simply use the sort method, with a block. You see, sorting inevitably comes down to a comparison between two adjacent elements. Using a block we can tell Ruby's sort method exactly how to make that comparison.
The double-ended arrow operator is simply Ruby's way of saying "compare". For less than it returns -1; for equals it returns 0; for greater than, 1. Sorting by first name is then as simple as:
And if we want to sort by both...
Any questions? |
Author: | wtd [ Fri Jun 24, 2005 8:33 pm ] | ||||
Post subject: | |||||
As a further example, let's represent a name as a class.
|