def from_sequence(cls, i, key=None):
        """Return the permutation needed to obtain ``i`` from the sorted
        elements of ``i``. If custom sorting is desired, a key can be given.

        Examples
        ========

        >>> Permutation.print_cyclic = True

        >>> Permutation.from_sequence('SymPy')
        Permutation(4)(0, 1, 3)
        >>> _(sorted('SymPy'))
        ['S', 'y', 'm', 'P', 'y']
        >>> Permutation.from_sequence('SymPy', key=lambda x: x.lower())
        Permutation(4)(0, 2)(1, 3)

        """
        ic = list(zip(i, range(len(i))))
        if key:
            ic.sort(key=lambda x: key(x[0]))
        else:
            ic.sort()
        return ~Permutation([i[1] for i in ic])