From 196269aa38b62546a46142821c54b04c78531fc9 Mon Sep 17 00:00:00 2001 From: jomo Date: Thu, 11 Aug 2022 02:49:22 +0200 Subject: [PATCH] fix sorting by type, id (natural) --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index e403a42..8f149a0 100644 --- a/main.py +++ b/main.py @@ -31,7 +31,7 @@ def _set_sqlite_pragma(conn, _): @app.get("/api/items", response_model=dict[str, Item]) async def list_items(db: Session = Depends(get_db)): # sort by type, id (natural) - natsort = lambda s: (s.type or '', [int(t) if t.isdigit() else t.lower() for t in re.split('(\d+)', s.id)]) + natsort = lambda item: [item.type or ''] + [int(t) if t.isdigit() else t.lower() for t in re.split('(\d+)', item.id)] items = crud.get_items(db) items = sorted(items, key=natsort) return {i.id:i for i in items}