Refactor to support MongoDB sorting.