The Shulhan Arukh (Orah Haim 32) writes that the parshiot of tefillin must be written "kesidran," i.e., in the order in which they appear in the Torah. Therefore the parasha of kadesh li kol behor (1) must be written before the parasha of vehaya ki yevia’cha (2), followed by the parasha of Shema (3), and finally, vehaya im shamo’a (4), as that is order in which they appear in the Torah. If the sofer writes the parshiot out of order, those parshiot are all invalid.
What if the sofer (scribe), incorrectly, wrote the parashot out of order? For example, if the first of the four parashot, kadesh li, was written last? The Poskim (see Magen Giborim 1) write that if he can find another copy of the first parasha, kadesh li, which was written in its proper order, before the next three parshiot, he may then connect this first parasha to the other parshiot (2-4). If not, the tefillin are invalid.
Interestingly, if the sofer is writing the tefillin of Rabbeinu Tam, in which the parshiot are placed in a different order, i.e. 1, 2, 4, 3, is customary to write the first two parshiot, leave a blank space, write the Shema (3), and then return and write the third parasha (4), in order to fulfill the requirement of "kesidran."
Regarding the order of the two tefillin, the Shulhan Arukh (32) rules that the shel yad should be written before the shel rosh. However, the Ari z"l insisted that the parshiot of the shel rosh should be written before the parshiot fo the shel yad. That is the custom of those who follow the view of the Ari z"l. Either way, the order does not impact upon the validity of the tefillin.