def sort_keys(self, keys, order=QuerySet.ASCENDING):
        missing_keys = [
            key
            for key in keys
            if not len(self._reverse_index[key])
        ]
        keys_and_values = [
            (key, self._reverse_index[key][0])
            for key in keys
            if key not in missing_keys
        ]
        sorted_keys = [
            kv[0]
            for kv in sorted(
                keys_and_values,
                key=lambda x: x[1],
                reverse=True if order == QuerySet.DESCENDING else False)
        ]
        if order == QuerySet.ASCENDING:
            return missing_keys + sorted_keys
        elif order == QuerySet.DESCENDING:
            return sorted_keys + missing_keys
        else:
            raise ValueError('Unexpected order value: {:d}'.format(order))