def search_results_sorted(self, key, document_list, reverse=False):
        """
        Sorting method of DMS search.

        Sorts documents making query of required data by itself from CouchDB.
        Appends documents that have no key to the end of the list in uncontrolled order.
        e.g. order they appear in iteration.

        @param key: Can be either "mdt_indexes" key name, e.g. "Employee"
                    or one of "metadata_created_date", "metadata_description", "metadata_doc_type_rule_id"
        @param document_list: List of document names to sort. e.g.: ['ADL-0001', 'CCC-0001', ... ]
        @param reverse: Direction of sorting (True/False)

        @return: Sorted list of document names using given @param key e.g.: ['CCC-0001', 'ADL-0001', ... ]
        """
        if document_list:
            values_list = []
            empty_values_list = []
            documents = self.get_sorting_docs_indexes(document_list)
            # Selecting proper sorting couch query params
            if key in ["metadata_created_date", "metadata_description", "metadata_doc_type_rule_id"]:
                doc_field = key
            else:
                doc_field = 'mdt_indexes'
            # Creating special, sorting capable list of tuples (Docname, Sorting field value)
            for doc in documents:
                if doc_field == 'mdt_indexes':
                    if key in doc[doc_field]:
                        value = doc[doc_field][key]
                    else:
                        value = ''
                else:
                    value = doc[key]
                if value:
                    values_list.append((doc.get_id, value))
                else:
                    empty_values_list.append(doc.get_id)
            try:
                document_list = sorted(values_list, key=itemgetter(1), reverse=reverse)
                document_list = map(lambda doc: doc[0],  document_list)
                document_list = document_list + empty_values_list
            except __HOLE__, e:
                log.error('sorting TypeError error in indexes: %s, in documents_list: %s' % (e, document_list))
                pass
        return document_list
dataset/ETHPy150Open adlibre/Adlibre-DMS/adlibre_dms/apps/core/search.py/DMSSearchManager.search_results_sorted