@staticmethod
def _sorted_shape_qhape(tensor=None, shape=None, qhape=None):
    """Sort `shape` and `qhape` according to ascending `qnum` along every
        index.  Used by `to_` and `from_ndarray`.

        Instead of explicitly giving a `shape` and a `qhape`, a tensor whose
        form data is to be used can also be given.
        """
    shape = tensor.shape if shape is None else shape
    qhape = tensor.qhape if qhape is None else qhape
    sorted_qhp = []
    sorted_shp = []
    for qim, dim in zip(qhape, shape):
        qim, dim = zip(*sorted(zip(qim, dim)))
        sorted_qhp.append(qim)
        sorted_shp.append(dim)
    return sorted_shp, sorted_qhp
