+def puzzle_sort_key(puzzle):
+ """Return an appropriate sort key for a puzzle in the database
+
+ The sort key must start with "puzzle-" to distinguish puzzle items
+ in the database from all non-puzzle items. After that, though, the
+ only requirements are that each puzzle have a unique key and they
+ give us the ordering we want. And for ordering, we want meta puzzles
+ before non-meta puzzles and then alphabetical order by name within
+ each of those groups.
+
+ So puting a "-meta-" prefix in front of the puzzle ID does the trick.
+ """
+
+ return "puzzle-{}{}".format(
+ "-meta-" if puzzle['type'] == "meta" else "",
+ puzzle['puzzle_id']
+ )
+