When can I swap around the order of operators?

I was doing this question:

Using $\left< x \middle| p\right> = \frac{1}{\sqrt{2 \pi \hbar}}e^{ipx/\hbar}$ show that:

$$\left<x \middle| \hat{p} \middle| \psi \right> = -i\hbar \frac{d}{dx} \left< x \middle| \psi\right>$$ for a general $\psi$.

Method 1 (how my lecturer did it)

\begin{align*} \left<x \middle| \hat{p} \middle| \psi \right> &= \int dp \, \left<x \middle| p\right> \left<p \middle| \hat{p} \middle| \psi \right> \\ &= \int dp \, p \frac{1}{\sqrt{2 \pi \hbar}}e^{ipx/\hbar} \left<p \middle|\psi\right> \\ &= \int dp \, (-i\hbar) \frac{d}{dx}\left<x \middle| p\right>\left<p\middle|\psi\right> \\ &= -i\hbar \frac{d}{dx}\left<x \middle|\psi\right> \end{align*}

1. Why do put it in integral form? (See below why I think it's unnessessary)
2. Why are we allowed to swap the operator order like we did in line 3?

Method 2 (how I did it seeing that we can just swap the order)

\begin{align*} \left<x \middle| \hat{p} \middle| \psi \right> &= \left<x \middle| p\right> \left<p \middle| \hat{p} \middle| \psi \right> \\ &= \hat{p} \left<x \middle|p\right>\left<p\middle|\psi\right> \\ &= \hat{p} \left<x \middle| \psi\right> \\ &= - i \hbar \frac{d}{dx} \left< x\middle| \psi\right> \end{align*}

I don't understand why putting it in integral form is even correct?

• Method 2 is an incorrect version of Method 1... – Phoenix87 May 15 '15 at 11:02
• Can you expand? How so? – turnip May 15 '15 at 11:02
• Method 1 is incorrect anyway. On the third line, the $\hat{p}$ should just be $p$, i.e. the eigenvalue of the operator $\hat{p}$ corresponding to the eigenvector $\lvert p\rangle$. However Method 2 is doubly incorrect, since you need to integrate over all $p$ to use the resolution of identity $1 = \int\mathrm{d}p\,\lvert p \rangle\langle p \rvert$. – Mark Mitchison May 15 '15 at 11:06
• You're right, that was just me mistyping it. I'll edit it. – turnip May 15 '15 at 11:07
• @PPG So do you understand now why there is no "swapping of operator order"? $p$ is not an operator. – Mark Mitchison May 15 '15 at 11:09

Your lecturer got the eigenvalue using the fact that the operator $\hat{p}$ is Hermitian so you can do this:
I think it becomes a bit neater if you put the projector $|p\rangle\langle p|$ after the operator $\hat{p}$ because then you don't have to do the gymnastics with the Hermitian conjugate. This would be my attempt (being as explicit as possible at each step):
Note that it is the number $p$ that is brought to the front in line two not the operator $\hat{p}$. We choose the states $|p\rangle$ so that we have the following equality: $\hat{p}|p\rangle = p|p\rangle$.