For the sake of ease, the Sefaria.org translation to the Talmud, tractate Megilla 17b, 18a will be quoted on this subject, even though improvements can be made to the translation.
The Gemara asks: And why did they see fit to institute that one says the blessing of the ingathering of the exiles after the blessing of the years? As it is written: “And you, O mountains of Israel, you shall shoot forth your branches, and yield your fruit to My people Israel; for they will soon be coming” (Ezekiel 36:8), which indicates that the ingathering of the exiles will follow after Eretz Yisrael is blessed with bountiful produce. And once the exiles have been gathered, judgment will be meted out to the wicked, as it is stated: “And I will turn my hand against you and purge away your dross as with lye” (Isaiah 1:25), and immediately after it is written: “And I will restore your judges as at first” (Isaiah 1:26). For this reason the blessing of the restoration of judges comes after the blessing of the ingathering of the exiles.
And once judgment is meted out to the wicked, the transgressors, i.e., the heretics and sectarians, will cease to be. Consequently, the next blessing is that of the heretics, and one includes evildoers with them, as it is stated: “And the destruction of the transgressors and of the sinners shall be together, and they that forsake the Lord shall cease to be” (Isaiah 1:28). The “transgressors and sinners” are the evildoers, and “they that forsake the Lord” are the heretics.
And once the heretics cease to be, the horn, i.e., the glory, of the righteous will be exalted, as it is written: “All the horns of the wicked will I cut off; but the horns of the righteous shall be exalted” (Psalms 75:11). Therefore, after the blessing of the heretics, one says the blessing about the righteous. And he includes the righteous converts along with the righteous, as it is stated: “You shall rise up before the hoary head, and honor the face of the elder” (Leviticus 19:32), and adjacent to this it is stated: “And if a stranger sojourns with you” (Leviticus 19:33). An “elder” is one with Torah wisdom and a “stranger” is one who has converted to Judaism.
And where will the horns of the righteous be exalted? In Jerusalem, as it is stated: “Pray for the peace of Jerusalem; they who love you shall prosper” (Psalms 122:6). “They who love you” are the righteous. Therefore, the blessing of the rebuilding of Jerusalem is placed after the blessing of the righteous.
And once Jerusalem is rebuilt, the Messiah, scion of the house of David, will come, as it is stated:
“Afterward the children of Israel shall return, and seek the Lord their God and David their king” (Hosea 3:5), and consequently, the blessing of the kingdom of David follows the blessing of the building of Jerusalem. And once the scion of David comes, the time for prayer will come, as it is stated: “I will bring them to My sacred mountain and make them joyful in My house of prayer” (Isaiah 56:7). Therefore, the blessing of hearing prayer is recited after the blessing of the kingdom of David.