how to decide on first coauthorship for a paper
Your colleague came up with the idea of combining the work of themselves and yourself into a single paper, and your co-author took the lead in writing the paper. For me, that would add up to enough distinct initiative to single out your colleague as the primary author of the paper.
You ask how &quot;to avoid such situation in the future&quot;; Roland's comment is spot-on in this regard: the author list and relative ordering therein should be discussed upfront, if you wish to discuss it at all.