From a08a2998a364bb4d5bb1ad1abae7f0c28fe9abce Mon Sep 17 00:00:00 2001 From: Correl Date: Thu, 15 Jul 2021 22:00:01 -0400 Subject: [PATCH] Sort search results --- tutor/database.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tutor/database.py b/tutor/database.py index 14b5423..868921a 100644 --- a/tutor/database.py +++ b/tutor/database.py @@ -141,12 +141,19 @@ async def advanced_search( joins.append("LEFT JOIN copies ON (cards.scryfall_id = copies.scryfall_id)") constraints.append("copies.id IS NULL") joins.append("JOIN sets ON (cards.set_code = sets.set_code)") + joins.append("JOIN rarities ON (cards.rarity = rarities.rarity)") query = " ".join( [ "SELECT cards.* FROM cards", " ".join(joins), "WHERE" if constraints else "", " AND ".join(constraints), + "ORDER BY rarities.rarity_ord DESC", + ", length(cards.color_identity) DESC", + ", CASE ", + " WHEN length(cards.color_identity) > 0 THEN '0'" + " ELSE cards.color_identity END ASC", + ", cards.name ASC", f"LIMIT {offset},{limit}", ] )