I know this topic is kind of depending on which school each researcher is from, so i would like to gain more information regarding the topic. I am a phd researcher and I have a specific task to do, a colleague of mine have similar task but using different agents. this colleague came up with an idea to compare both agents and write a paper about it, s\he performed his\her experiments and started writing the paper, s\he then approached me with the idea to compare, so I agreed, performed and analyzed my data, which took me weeks to finish, and shared it with this colleague. As I was working on another manuscript and s\he wanted to write this new manuscript by him\herself, I only contributed to read and correct writing-wise (plus of course my data and results) but the most recent version was never shared with me, last one shared was around Nov-Dec 2020, and till this moment I dont know how my contribution was written in the author statement. I got to have a fast look at paper after submission and saw that the graphs always had one part regarding my data, yet i am only a co-author and this colleague is the first author. My question here, was I supposed to have an equal contribution and supposed to be a first co-author? my other colleagues pointed this out and that s\he should have put me as a first coauthor with him\her, and I am not sure if its true or not, i dont have much experience writing manuscripts, and my (our) SV never pointed this out to me. Now its anyhow late to ask for a coauthorship, but I would like to avoid such situation in the future, so would really appreciate if someone has an experience on how to handle such situation. Sorry for the long message and thanks in advance!
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 "to avoid such situation in the future"; 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.
Speaking as somebody who reads a lot of papers, my only desire is that it should be clear who did what on a paper, because if want to contact the authors I want to know who to contact.
In general, whoever WRITES the majority of the paper should be the first author. If some person did some critical task that was more important than writing the paper, then to my mind the way to handle that is to make them the second author but put a note in the paper describing their contribution ("Dr. Smith spent five years hand nursing over 50 ferrets to make these experiments possible... etc"). That way the reader knows who did what.