From ee760902652afcb0852c4a9b843833ea6d087baa Mon Sep 17 00:00:00 2001 From: Froidefond Julien Date: Fri, 6 Mar 2026 21:59:19 +0100 Subject: [PATCH] fix: natural sort regex to handle titles with letters after numbers Replace REGEXP_REPLACE with REGEXP_MATCH to extract only digits Fixes 'invalid input syntax for type integer' error when titles contain letters after numbers like '20th century boys' --- apps/api/src/books.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/api/src/books.rs b/apps/api/src/books.rs index 216db31..0194f83 100644 --- a/apps/api/src/books.rs +++ b/apps/api/src/books.rs @@ -107,7 +107,7 @@ pub async fn list_books( REGEXP_REPLACE(LOWER(title), '[0-9]+', '', 'g'), -- Extract first number group and convert to integer for numeric sort COALESCE( - NULLIF(REGEXP_REPLACE(LOWER(title), '^[^0-9]*', '', 'g'), '')::int, + (REGEXP_MATCH(LOWER(title), '\d+'))[1]::int, 0 ), -- Then by full title as fallback @@ -253,7 +253,7 @@ pub async fn list_series( PARTITION BY COALESCE(NULLIF(series, ''), 'unclassified') ORDER BY REGEXP_REPLACE(LOWER(title), '[0-9]+', '', 'g'), - COALESCE(NULLIF(REGEXP_REPLACE(LOWER(title), '^[^0-9]*', '', 'g'), '')::int, 0), + COALESCE((REGEXP_MATCH(LOWER(title), '\d+'))[1]::int, 0), title ASC ) as rn FROM books @@ -277,7 +277,7 @@ pub async fn list_series( REGEXP_REPLACE(LOWER(sc.name), '[0-9]+', '', 'g'), -- Extract first number group and convert to integer COALESCE( - NULLIF(REGEXP_REPLACE(LOWER(sc.name), '^[^0-9]*', '', 'g'), '')::int, + (REGEXP_MATCH(LOWER(sc.name), '\d+'))[1]::int, 0 ), sc.name ASC