All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
There is complete agreement among scholars of Islam concerning the legitimacy of making rows straight while performing prayers, but they differ on whether such an action is obligatory. According to the majority of Muslim scholars, it is desirable to straighten lines in prayers. On the other hand, it is a duty to make rows straight according to Dawood Ibn 'Ali, Ibn Hazm and those who follow them.
In addition, it is desirable to stand shoulder-to-shoulder and foot-to-foot. This is based on the Hadeeth from Anas that the Prophet said: “Straighten your rows since I see you from behind my back [this is one of the Prophet's peculiarities].” [Al-Bukhari]
Anas said: “We used to join one's shoulder to the other's shoulder and one's foot to the other's foot.”
Another Hadeeth reads: “Complete your rows and put your shoulders close to each other, fill in the gaps and leave no space for the Devil.” [Abu Daawood, Ibn Khuzaymah and al-Haakim classified it as sound]
For more benefit on aligning the rows during prayer, please refer to Fataawa 322945, 89439, and 361669.
Allah knows best.