$\mathrm{Tr}(XY) = \mathrm{Tr}(YX)$?

I'm trying to understand the Dirac notation to understand quantum mechanics better. I'm trying to show the above relation using the Dirac notation.

Given

$$\mathrm{Tr}(X)~=~\sum_j\langle j|X|j\rangle$$ and $$\mathrm{Tr}(Y)~=~\sum_j\langle j|Y|j\rangle.$$ Thus,$$\mathrm{Tr}(XY)~=~\sum_{ij}\langle j|X|i\rangle \langle i|Y|j\rangle.$$ And isn't that just equal to the following $$\mathrm{Tr}(XY)~=~\sum_j \langle j|XY|j\rangle?$$ But that shouldn't equal the following $$\mathrm{Tr}(XY)~=~\sum_j\langle j|YX|j\rangle,$$ since $XY \neq YX$, right?

Remember that $\Sigma_i |i \rangle \langle i |$ is the identity operator.

Then $Tr(XY) = \Sigma_j \langle j| XY |j \rangle = \Sigma_j \langle j| X \left(\Sigma_i |i \rangle \langle i |\right)Y |j \rangle = \Sigma_j \Sigma_i \langle j| X |i \rangle \langle i |Y |j \rangle =\Sigma_j \Sigma_i \langle i |Y |j \rangle \langle j| X |i \rangle = \Sigma_i \Sigma_j \langle i |Y |j \rangle \langle j| X |i \rangle = \Sigma_i \langle i |Y \left( \Sigma_j |j \rangle \langle j | \right) X |i \rangle =\Sigma_i \langle i |Y X |i \rangle = Tr(YX)$.

So the two traces are equal.

Throwing a wrench in the works, we mention that

$${\rm Tr}[X,Y]~=~0$$

does not always hold for two linear operators $$X:D(X)\subseteq H~\to~ H\qquad\text{and}\qquad Y:D(Y)\subseteq H~\to~ H$$ in an infinite-dimensional vector space $H$. This is not just an academic remark. Take for instance the trace on both sides of the canonical commutation relations

$$[\hat{x},\hat{p}]~=~i\hbar{\bf 1},$$

cf. Example 1 in Ref. 1 and this Phys.SE post.

References:

1. F. Gieres, Mathematical surprises and Dirac’s formalism in quantum mechanics, arXiv:quant-ph/9907069. (Hat tip: David Bar Moshe.)

To extend @NowIGetToLearnWhatAHe's answer, it should be noted that it's a general theorem of linear algebra that traces are cyclical, so it's always true that:

$$Tr(ABCDE...XYZ) = Tr(ZABCDE...XY)$$

From this, your result follows as a special case.