1import sqlite3 2 3def collate_reverse(string1, string2): 4 if string1 == string2: 5 return 0 6 elif string1 < string2: 7 return 1 8 else: 9 return -1 10 11con = sqlite3.connect(":memory:") 12con.create_collation("reverse", collate_reverse) 13 14cur = con.cursor() 15cur.execute("create table test(x)") 16cur.executemany("insert into test(x) values (?)", [("a",), ("b",)]) 17cur.execute("select x from test order by x collate reverse") 18for row in cur: 19 print(row) 20con.close() 21