I want to sort alphabetically all CRSs in the Recently Used Coordinate Reference Systems list in QGIS. But I can't find a way to do this using the user interface.
How one can sort them using PyQGIS?
Sorting Recently Used Coordinate Reference Systems list cloud be done using the QgsCoordinateReferenceSystemRegistry class.
Here is a code that automatically sorts it alphabetically using CRS name when a new CRS is pushed to the top of the list.
Place this code in a startup.py file can be found under C:\Users\%USERNAME%\AppData\Roaming\QGIS\QGIS3\startup.py (for Windows 10 users):
def sort_recent_crs():
recent_crs_list = qgis.core.QgsApplication.coordinateReferenceSystemRegistry().recentCrs()
recent_crs_dict_disordered = {crs.description(): crs for crs in recent_crs_list}
sorted_dict = {key: recent_crs_dict_disordered[key] for key in sorted(recent_crs_dict_disordered)}
sorted_values = [value for value in sorted_dict.values()]
qgis.core.QgsApplication.coordinateReferenceSystemRegistry().recentCrsPushed.disconnect(sort_recent_crs)
for crs in sorted_values[::-1]:
qgis.core.QgsApplication.coordinateReferenceSystemRegistry().pushRecent(crs)
qgis.core.QgsApplication.coordinateReferenceSystemRegistry().recentCrsPushed.connect(sort_recent_crs)
qgis.core.QgsApplication.coordinateReferenceSystemRegistry().recentCrsPushed.connect(sort_recent_crs)