diff --git a/datasette/views/table.py b/datasette/views/table.py
index 96e8922b..7267c1db 100644
--- a/datasette/views/table.py
+++ b/datasette/views/table.py
@@ -402,8 +402,12 @@ class TableView(RowTableShared):
)
# Allow for custom sort order
- sort = special_args.get("_sort") or table_metadata.get("sort")
- sort_desc = special_args.get("_sort_desc") or table_metadata.get("sort_desc")
+ sort = special_args.get("_sort")
+ sort_desc = special_args.get("_sort_desc")
+
+ if not sort and not sort_desc:
+ sort = table_metadata.get("sort")
+ sort_desc = table_metadata.get("sort_desc")
if sort and sort_desc:
raise DatasetteError("Cannot use _sort and _sort_desc at the same time")
diff --git a/tests/test_html.py b/tests/test_html.py
index a0216672..d54446c7 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -1105,7 +1105,7 @@ def test_metadata_sort(app_client):
ths = table.findAll("th")
assert ["id", "name\xa0▼"] == [th.find("a").string.strip() for th in ths]
rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
- assert [
+ expected = [
[
'
3 | ',
'Detroit | ',
@@ -1122,7 +1122,14 @@ def test_metadata_sort(app_client):
'1 | ',
'San Francisco | ',
],
- ] == rows
+ ]
+ assert expected == rows
+ # Make sure you can reverse that sort order
+ response = app_client.get("/fixtures/facet_cities?_sort_desc=name")
+ assert response.status == 200
+ table = Soup(response.body, "html.parser").find("table")
+ rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
+ assert list(reversed(expected)) == rows
def test_metadata_sort_desc(app_client):
@@ -1133,7 +1140,7 @@ def test_metadata_sort_desc(app_client):
ths = table.findAll("th")
assert ["pk\xa0▲", "name"] == [th.find("a").string.strip() for th in ths]
rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
- assert [
+ expected = [
[
'2 | ',
'Paranormal | ',
@@ -1142,4 +1149,11 @@ def test_metadata_sort_desc(app_client):
'1 | ',
'Museum | ',
],
- ] == rows
+ ]
+ assert expected == rows
+ # Make sure you can reverse that sort order
+ response = app_client.get("/fixtures/attraction_characteristic?_sort=pk")
+ assert response.status == 200
+ table = Soup(response.body, "html.parser").find("table")
+ rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
+ assert list(reversed(expected)) == rows