Which sefer did the Ramabm author first, was it his Mishnah Torah (Yad HaChazaka) or his commentary to Mishnayos?
The Commentary on the Mishnah came first. In his colophon at the end of it, Rambam writes that he began writing the commentary at age 23, and finished it at age 30, in the year 1479 of the "Era of Documents" (4928 since Creation, 1168 CE).
The Mishneh Torah, on the other hand, was written in the 4930s. In the introduction he says that the current year is 4937; in Hilchos Kiddush Hachodesh he gives examples using the years 4930 and 4938 (chs. 9 and 11ff, respectively), and in Hilchos Shemittah VeYovel (10:4) he says that the current year is 4936.
After having completed our previous well-known work wherein we included a commentary to the whole Mishnah – our goal in that work having been satisfied with the explanation of the substance of each and every Halacha in the Mishnah, since our intention there was not to include an exhaustive discussion of the law of every Commandment which would embrace all that is necessary (to know) of the prohibited and the permissible, liable and free, as will be made clear to him who studies that work – I deemed it advisable to compile a compendium which would include all the laws of the Torah and its regulations, nothing missing in it. (Chavel translation, my emphasis)