Dev Helpers
← Back

Prioritize names using an external sort order

Prioritize names using an external sort order using Python lists and dictionaries.

Open this example in the interactive library

Source code

order = {name: i for i, name in enumerate(['critical', 'normal'])}
jobs = [('low', 3), ('normal', 2), ('critical', 1)]
print(sorted(jobs, key=lambda job: order.get(job[0], len(order))))

Sample output

[('critical', 1), ('normal', 2), ('low', 3)]