The body of this method is very similar to sortDocs, would it possible somehow to share more code with sortDocs?