Repository: nautobot/nautobot

Issue:
Search Fields in Advanced Filter Form sorted alphabetically
<!--
    NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED.

    This form is only for reporting reproducible bugs. If you need assistance
    with Nautobot installation, or if you have a general question, please start a
    discussion instead: https://github.com/nautobot/nautobot/discussions

    Please describe the environment in which you are running Nautobot. Be sure
    that you are running an unmodified instance of the latest stable release
    before submitting a bug report, and that any plugins have been disabled.
-->
### Environment
* Nautobot version (Docker tag too if applicable): 1.5.2
* Python version: 3.10
* Database platform, version: -
* Middleware(s): -

<!--
    Describe in detail the exact steps that someone else can take to reproduce
    this bug using the current stable release of Nautobot. Begin with the
    creation of any necessary database objects and call out every operation
    being performed explicitly. If reporting a bug in the REST API, be sure to
    reconstruct the raw HTTP request(s) being made: Don't rely on a client
    library such as pynautobot.
-->
### Steps to Reproduce
1. Click the "Filter" button on any list view
2. Click the "Advanced" tab
3. Click a field drop-down
4. Try search in the drop-down menu

<!-- What did you expect to happen? -->
### Expected Behavior
I expect the values in drop-down menu to be sorted alphabetically

<!-- What happened instead? -->
### Observed Behavior
It is not comfortable to go through all values each time to find the search filter.


Discussion:

Resolving patch:
diff --git a/nautobot/utilities/forms/forms.py b/nautobot/utilities/forms/forms.py
--- a/nautobot/utilities/forms/forms.py
+++ b/nautobot/utilities/forms/forms.py
@@ -329,7 +329,7 @@ def _get_lookup_field_choices(self):
             for name, field in self.filterset_filters.items()
             if "__" not in name and name != "q"
         )
-        return sorted(filterset_without_lookup)
+        return sorted(filterset_without_lookup, key=lambda x: x[1])
 
 
 def dynamic_formset_factory(filterset_class, data=None, **kwargs):


Test patch:
diff --git a/nautobot/utilities/tests/test_forms.py b/nautobot/utilities/tests/test_forms.py
--- a/nautobot/utilities/tests/test_forms.py
+++ b/nautobot/utilities/tests/test_forms.py
@@ -691,7 +691,6 @@ def test_dynamic_filter_form(self):
                 site_form._get_lookup_field_choices(),
                 [
                     ("asn", "ASN"),
-                    ("cf_example_plugin_auto_custom_field", "Example Plugin Automatically Added Custom Field"),
                     ("circuit_terminations", "Circuit terminations"),
                     ("clusters", "Clusters"),
                     ("comments", "Comments"),
@@ -701,6 +700,7 @@ def test_dynamic_filter_form(self):
                     ("created", "Created"),
                     ("description", "Description"),
                     ("devices", "Devices (name or ID)"),
+                    ("cf_example_plugin_auto_custom_field", "Example Plugin Automatically Added Custom Field"),
                     ("facility", "Facility"),
                     ("has_circuit_terminations", "Has circuit terminations"),
                     ("has_clusters", "Has clusters"),
@@ -723,16 +723,16 @@ def test_dynamic_filter_form(self):
                     ("prefixes", "Prefixes"),
                     ("rack_groups", "Rack groups (slug or ID)"),
                     ("racks", "Racks"),
-                    ("region", "Region (slug)"),
                     ("region_id", "Region (ID)"),
+                    ("region", "Region (slug)"),
                     ("shipping_address", "Shipping address"),
                     ("slug", "Slug"),
                     ("status", "Status"),
                     ("tag", "Tags slug"),
+                    ("tenant_id", 'Tenant (ID) (deprecated, use "tenant" filter instead)'),
                     ("tenant", "Tenant (slug or ID)"),
-                    ("tenant_group", "Tenant Group (slug)"),
                     ("tenant_group_id", "Tenant Group (ID)"),
-                    ("tenant_id", 'Tenant (ID) (deprecated, use "tenant" filter instead)'),
+                    ("tenant_group", "Tenant Group (slug)"),
                     ("time_zone", "Time zone"),
                     ("vlan_groups", "Vlan groups (slug or ID)"),
                     ("vlans", "Vlans"),