Lazycoder

16Mar/050

Ruby’s Enumerable sort_by adventures in Ruby part 3

Ruby’s Enumerable sort_by

Derek Sivers points to another cool thing in the Ruby language. The Enumerable->sort_by method.

It allows you to sort an Enumerable object by a property of the objects it contains. More specifically, it allows you to sort by a block of code you pass in.

So, for example, I could sort my collection of blog entries by the date posted and output the title of the first 10 and create a block that displays the last 10 posts.