'Returns a ``TIDS`` with sorted components
The sorting is done taking into account the commutation group
of the component tensors.'

def sorted_components(self):

    from sympy.combinatorics.permutations import _af_invert
    cv = list(zip(self.components, range(len(self.components))))
    sign = 1
    n = (len(cv) - 1)
    for i in range(n):
        for j in range(n, i, (-1)):
            c = cv[(j - 1)][0].commutes_with(cv[j][0])
            if (c not in [0, 1]):
                continue
            if ((cv[(j - 1)][0].index_types, cv[(j - 1)][0]._name) > (cv[j][0].index_types, cv[j][0]._name)):
                (cv[(j - 1)], cv[j]) = (cv[j], cv[(j - 1)])
                if c:
                    sign = (- sign)
    components = [x[0] for x in cv]
    perm_inv = [x[1] for x in cv]
    perm = _af_invert(perm_inv)
    free = [(ind, i, perm[c]) for (ind, i, c) in self.free]
    free.sort()
    dum = [(i1, i2, perm[c1], perm[c2]) for (i1, i2, c1, c2) in self.dum]
    dum.sort(key=(lambda x: components[x[2]].index_types[x[0]]))
    return (TIDS(components, free, dum), sign)
