@ElenaS I too have the same problem, the only way I have gotten around it is to save each query expression to a file, delete all of the queries from the layer, rename the .exp files that you saved to reflect the order that you want, then use the "Add definition queries from files..." method (it is on the drop down for "New definition query"). When the dialog comes up to pick your file(s), you will already have them in order based on how you renamed them, so you pick the first file, hold down your shift key, and then choose the last file. Alternatively, you can also use your CTRL key to pick the order that you want, even if you didn't rename the .exported .exp files. This way you can keep the name of the query like you want it to show as in the list, and you can specify the order from how you pick them.
Agree with @ElenaS
💡Would like the functionality to manually sort Definition Queries (click, drag) or else alphabetize (by Query name) in the Data/Definition Query drop-down list.
In addition, would like the functionality to AUTO-Zoom to Selection from drop-down list. For example, the list contains queries selecting City Parks by Name (alphabetically, hopefully) and when a particular City Park name is selected from the list, the map will automatically Zoom To Selection on that City Park layer.
Thanks.
@RandyWeaver: Oh, smart! I'll definitely use that next time.
@pkorst_coa: I agree an auto-zoom feature would be nice. Rather than Zoom to Selection though, Zoom to Layer might be more relevant to avoid an additional step (selection).
Also, in case it helps, I added the Zoom To Layer command into my Quick Access Toolbar to make it a little bit more accessible for now.
For anyone still looking for this (like I did an hour ago): I wrote a python snippet that will sort definition queries in alphabetical order by query name. It can be used in the python window in an ArcGIS Pro Project (Tested in 3.1.1).
To sort definition queries, paste the below code into the python window and replace Name of Map and Name of Layer (or Name of Table) in lines 2 and 3 with the names of your map and the layer/table where you want to sort the queries (keep the quotation marks). Then press enter twice to run the code.
For a single layer:
aprx = arcpy.mp.ArcGISProject('CURRENT')
mp = aprx.listMaps('Name of Map')[0]
lyr = mp.listLayers('Name of Layer')[0]
if lyr.supports('DEFINITIONQUERY'):
dq = lyr.listDefinitionQueries()
#print(dq)
sorted_dq = sorted(dq, key=lambda x: (x['name'].upper(), x['name'].upper()))
#print(sorted_dq)
lyr.updateDefinitionQueries(sorted_dq)
For a single standalone table:
aprx = arcpy.mp.ArcGISProject('CURRENT')
mp = aprx.listMaps('Name of Map')[0]
tbl = mp.listTables('Name of Table')[0]
dq = tbl.listDefinitionQueries()
#print(dq)
sorted_dq = sorted(dq, key=lambda x: (x['name'].upper(), x['name'].upper()))
#print(sorted_dq)
tbl.updateDefinitionQueries(sorted_dq)
If you have already executed the code for one layer or table and want to do another one in the same map, you don't need lines 1 and 2 (the variables are still saved in the system – but it won't break anything if you define them again).
You can also sort the definition queries in all layers and tables in the map:
aprx = arcpy.mp.ArcGISProject('CURRENT')
mp = aprx.listMaps('Name of Map')[0]
lyrs = mp.listLayers()
for lyr in lyrs:
if lyr.supports('DEFINITIONQUERY'):
dq = lyr.listDefinitionQueries()
if len(dq)>1:
sorted_dq = sorted(dq, key=lambda x: (x['name'].upper(), x['name'].upper()))
lyr.updateDefinitionQueries(sorted_dq)
tbls = mp.listTables()
for tbl in tbls:
dq = tbl.listDefinitionQueries()
if len(dq)>1:
sorted_dq = sorted(dq, key=lambda x: (x['name'].upper(), x['name'].upper()))
tbl.updateDefinitionQueries(sorted_dq)
Have fun 🙂
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.