كتاب الْمَسَاجِدِ وَمَوَاضِعِ الصَّلاَةِ
5  
The Book of Mosques and Places of Prayer
(53)
Chapter: Who is more entitled to lead the prayer
(53)
باب مَنْ أَحَقُّ بِالإِمَامَةِ
Sahih Muslim 673 a

Abu Mas'ud al-Ansari reported Allah's Messenger (ﷺ) as saying:

The one who is most versed in Allah's Book should act as Imam for the people, but If they are equally versed in reciting it, then the one who has most knowledge regarding Sunnah if they are equal regarding the Sunnah, then the earliest one to emigrate; it they emigrated at the same time, then the earliest one to embrace Islam. No man must lead another in prayer where (the latter) has authority, or sit in his place of honour in his house, without his permission. Ashajj in his narration used the word," age" in place of" Islam".
وَحَدَّثَنَا أَبُو بَكْرِ بْنُ أَبِي شَيْبَةَ، وَأَبُو سَعِيدٍ الأَشَجُّ كِلاَهُمَا عَنْ أَبِي خَالِدٍ، - قَالَ أَبُو بَكْرٍ حَدَّثَنَا أَبُو خَالِدٍ الأَحْمَرُ، - عَنِ الأَعْمَشِ، عَنْ إِسْمَاعِيلَ بْنِ رَجَاءٍ، عَنْ أَوْسِ بْنِ ضَمْعَجٍ، عَنْ أَبِي مَسْعُودٍ الأَنْصَارِيِّ، قَالَ قَالَ رَسُولُ اللَّهِ صلى الله عليه وسلم ‏ "‏ يَؤُمُّ الْقَوْمَ أَقْرَؤُهُمْ لِكِتَابِ اللَّهِ فَإِنْ كَانُوا فِي الْقِرَاءَةِ سَوَاءً فَأَعْلَمُهُمْ بِالسُّنَّةِ فَإِنْ كَانُوا فِي السُّنَّةِ سَوَاءً فَأَقْدَمُهُمْ هِجْرَةً فَإِنْ كَانُوا فِي الْهِجْرَةِ سَوَاءً فَأَقْدَمُهُمْ سِلْمًا وَلاَ يَؤُمَّنَّ الرَّجُلُ الرَّجُلَ فِي سُلْطَانِهِ وَلاَ يَقْعُدْ فِي بَيْتِهِ عَلَى تَكْرِمَتِهِ إِلاَّ بِإِذْنِهِ ‏"‏ ‏.‏ قَالَ الأَشَجُّ فِي رِوَايَتِهِ مَكَانَ سِلْمًا سِنًّا ‏.‏
Reference : Sahih Muslim 673a
In-book reference : Book 5, Hadith 365
USC-MSA web (English) reference : Book 4, Hadith 1420
  (deprecated numbering scheme)