The following hadith and excerpt demonstrate that the order of prayer rows is based on rank of understanding, wisdom, prudence, sedation. Since the one who has all these qualities follows the sunnah the best and most perfect, the order must take into account Sunnah in ones lifestyle. The nearest to the Prophet (S) are the ones who are nearest to him inwardly and outwardly.
–
The Messenger of AllÄh, peace and blessings of AllÄh be on him, used to touch our shoulders at the time of prayer, and used to say:
“Keep straight and do not be uneven, for in that case your hearts would disagree. Let those from among you, who are possessed of understanding and wisdom, stand nearest to me, then those who are next to them, then those who are next to them.”
- Sahih Muslim, Mishkat 4:24
Abdullah b. Mas’ud reported: The Messenger of Allah (may peace be upon him) said: Let those who are sedate and prudent be near me, then those who are next to them (saying it three times), and beware of the tumult of the markets.
Sahih Muslim Book 4, Number 870:
Anas reported on the authority of the Prophet, peace and blessings of AllÄh be on him, (who) said:
“Arrange your ranks properly, for the proper arrangement of ranks is part of the keeping up of prayer.” (Bukhari )
Mukhtasar al-Quduri – Hanafi Fiqh Manual
The men line up, and then behind them the boys, and then the women at the back.