void DocumentSourceSort::populate() {
    if (_mergingPresorted) {
        typedef DocumentSourceMergeCursors DSCursors;
        if (DSCursors* castedSource = dynamic_cast<DSCursors*>(pSource)) {
            populateFromCursors(castedSource->getCursors());
        } else {
            msgasserted(17196, "can only mergePresorted from MergeCursors");
        }
    } else {
        while (boost::optional<Document> next = pSource->getNext()) {
            loadDocument(std::move(*next));
        }
        loadingDone();
    }
}
void DocumentSourceSort::populate() {
    if (_mergingPresorted) {
        typedef DocumentSourceMergeCursors DSCursors;
        typedef DocumentSourceCommandShards DSCommands;
        if (DSCursors* castedSource = dynamic_cast<DSCursors*>(pSource)) {
            populateFromCursors(castedSource->getCursors());
        } else if (DSCommands* castedSource = dynamic_cast<DSCommands*>(pSource)) {
            populateFromBsonArrays(castedSource->getArrays());
        } else {
            msgasserted(17196, "can only mergePresorted from MergeCursors and CommandShards");
        }
    } else {
        scoped_ptr<MySorter> sorter (MySorter::make(makeSortOptions(), Comparator(*this)));
        while (boost::optional<Document> next = pSource->getNext()) {
            sorter->add(extractKey(*next), *next);
        }
        _output.reset(sorter->done());
    }
    populated = true;
}