Reducing features to 2 by PCA confusion Matlab
After obtaining the principal component, you should compute $XW$ rather than $X^TXW$ where $W$ is the principal component.
Remark about computation efficiency: As you sort the eigenvalues, you could have obtained the corresponding permutation indices.