Problem 5 If
$$
f(x)=\left|x-a_{1}\right|-\left|x-a_{2}\right|+\left|x-a_{3}\right|\left(a_{1}<a_{2}<a_{3}\right) \text {, }
$$

find the maximum and minimum values of this function.
Since there is no intuitive geometric meaning, let's use the algebraic method.

First, remove the absolute value and convert it into a piecewise function to find the maximum and minimum values. It is easy to get,
$$
f(x)=\left\{\begin{array}{ll}
-x+a_{1}-a_{2}+a_{3}, & x \leqslant a_{1} ; \\
x-a_{1}-a_{2}+a_{3}, & a_{1}<x \leqslant a_{2} ; \\
-x+a_{1}+a_{2}-a_{3}, & a_{2}<x \leqslant a_{3} ; \\
x-a_{1}+a_{2}-a_{3}, & x > a_{3} .
\end{array}\right.
$$

It can be seen that the graph is in the shape of a “$W$”.
Therefore, there is no maximum value, and the minimum value can only be obtained at the breakpoints.
Consider the extreme values
$$
\begin{array}{l}
f\left(a_{1}\right)=-a_{2}+a_{3}, \\
f\left(a_{2}\right)=-a_{1}+a_{3}, \\
f\left(a_{3}\right)=-a_{1}+a_{2} .
\end{array}
$$

It is easy to know that, if $2 a_{1} \geqslant a_{2}+a_{3}$, then
$$
f(x)_{\text {min }}=f\left(a_{1}\right) \text {; }
$$

If $2 a_{2}<a_{1}+a_{3}$, then $f(x)_{\text {min }}=f\left(a_{3}\right)$.