Consider this situation.
A research plan is formulated for a masters student (M) collectively by a professor (P) and a PhD student (G). M runs the experiments but has no idea how to extract knowledge from his experiments (he is not motivated enough to make the effort). P asks G to 'look into it'. G analyzes the data, studies the subject background, identifies some erroneous numbers and re-runs some experiments. G then drafts a research manuscript for submission to a journal.
Even though the 'raw' data was acquired by M, it was G who made efforts to discuss the results and transform the data into a manuscript. M argues that since the research plan was a part of his masters dissertation, he should be the first author. To resolve the dispute, P keeps himself the first author, G the second, and M the third.
Who should be the first author in this case? (The first authorship matters in the field in question.)