I tried doing the following integral: $\int_{0}^{\pi/4}\sqrt{1-\sin2x}\mathrm dx$. Firstly I completed the square by rewriting $1$ as $\sin^2x+\cos^2x$ to get the integral revised to this form: $$I=\int_{0}^{\pi/4}\left|\cos x-\sin x\right|\mathrm dx$$ Now clearly I can figure out which trig function is greater on that interval to get rid of that absolute value sign. But let's assume had it been an indefinite integral and I was to find the general antiderivative of the integrand.
I tried putting it into Integral Calculator which pulled out $\mathrm{sgn}(\cos x-\sin x)$ and simplified the integral as follows: $$\int \left|\cos x-\sin x\right|\mathrm dx=\mathrm{sgn}(\cos x-\sin x)\int(\cos x-\sin x)\mathrm dx$$
My query is: Can it be done for all integrands contained in absolute value expressions? Does it work for $\int \left|x\right|\mathrm dx$ or literally any other expression? I would love to get details as to why it works if it does, maybe in the form of a proof. Many thanks.
Edit: As Gerry Myerson pointed out that this formula: $\int \left|f'(x)\right|\mathrm dx=f(x)\mathrm{sgn}(f(x))$ does not always hold in the link shared by Yuito Cheng in the comments. I would like to follow up to the original question.
Under what conditions on $f'(x)$ is this formula valid?