Port sorted-mongoid code

Tests are failing because a mongoid for rails4 is still in development.