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.