Why are the lengths the way they are with middle finger the longest?
When you bend your fingers, they all arrive at the same height. I would tend to think that their lengths differ when you keep them straight because they need to have similar length when you bend them. And I guess that there is some optimal length (when bended) for fingers to grasp things.
It is nothing but a quick hypothesis. But it sounds quite plausible to me.
As @JordiZambrino commented, it is possible also that middle fingers evolved to be longer in reaction for the need for throwing things.