fix sorting by type, id (natural)

This commit is contained in:
jomo 2022-08-11 02:49:22 +02:00
parent 160c12561c
commit 196269aa38

View file

@ -31,7 +31,7 @@ def _set_sqlite_pragma(conn, _):
@app.get("/api/items", response_model=dict[str, Item]) @app.get("/api/items", response_model=dict[str, Item])
async def list_items(db: Session = Depends(get_db)): async def list_items(db: Session = Depends(get_db)):
# sort by type, id (natural) # 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 = crud.get_items(db)
items = sorted(items, key=natsort) items = sorted(items, key=natsort)
return {i.id:i for i in items} return {i.id:i for i in items}