A different sort

20 Dec

Turns out it’s basically a ‘sort’ operator, borrowed from Perl:

results.sort! { |a, b| a.lname <=> b.lname }

In this case, from an example I was studying and expanding upon here, ‘results’ is an array of similar Person objects each of which have an ‘fname’ and ‘lname’ attribute for first and last name respectively.  I wanted to sort them by last name before printing to the screen and found the syntax illustrated above as a solution.  Lo and behold, there’s my little UFO, just in time to sort things out before the Christmas rush and that pesky Mayan apocalypse from Planet X.

Advertisements

Comments are welcome!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: