def sort(self, key='sum', axis=0, ascending=True):
        if key == 'sum':
            agg = self.sum(int(not axis))
        elif key == 'norm':
            agg = self.norm(int(not axis))
        elif isinstance(key, IndexMatrix) and key.is_vector():
            agg = key
        elif callable(key):
            agg = key(self, axis=int(not axis))
        else:
            raise TypeError("Key needs to be either one of the functions, or a suitable function.")

        if ascending:
            ascending = 1
        else:
            ascending = -1

        sorted_indices = np.argsort(agg.matrix.todense().getA().flatten()).tolist()[::ascending]

        res = None
        if axis == 0:
            res = self[sorted_indices]
        elif axis == 1:
            res = self[:, sorted_indices]

        return res
Applies key function on either rows or columns, and sorts the matrix based on the resulting vector.
dataset/ETHPy150Open jimmycallin/pydsm/pydsm/indexmatrix.py/IndexMatrix.sort