Ruby uses some kind of Quicksort to sort its arrays. Unfortunately Quicksort is not stable. When you get sorted data from your database, which almost certainly sorts faster than anything in Ruby, you sometimes may want the data to be resorted in a stable way.
I found following snippet:
I extended it to accept a block like the original sort method does: