What is right and what is commonly done can be quite different. Both depend somewhat on the field and maybe other things. If a group of authors is large, then a seemingly small change might have a big impact on who is judged first.
You can, and probably should, talk to the PI and ask for the reasoning. You may not be happy with the reasons given, but if you are a student, you may need to accept the decision even if it bothers you. It is even possible, if not exactly right, that the PI is trying to give a boost to someone ready to finish.
If the PI is favoring someone now, maybe it will be yourself when you are close to finishing. Note that the last is pure speculation and may not apply at all, of course.
I'll give two bits of advice, though they apply more to the future than to this particular paper. First, try to work out authorship questions early rather than late. Students may not even have the power to do this in a large group, but those with relatively equal academic standing should always work this out at the start of a collaboration in fields in which it matters.
Second, and more important, the paper in question is not likely to be either the best or last publication under your name. Any authorship is a plus for a beginning academic. Before you escalate and get people upset, think about your long term goals and how best to reach them. Having a supportive PI and a circle of collaborators is a good thing. Avoid being academically abused, of course, and if it is a repeated thing, then you are probably in the wrong group.
But for a student, and maybe a post-doc, what you can do effectively in such situations while preserving and advancing your career is limited. You are (too) dependent on the good will of the PI.