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'
This commit is contained in:
2026-03-06 21:59:19 +01:00
parent d86301919d
commit ee76090265

View File

@@ -107,7 +107,7 @@ pub async fn list_books(
REGEXP_REPLACE(LOWER(title), '[0-9]+', '', 'g'), REGEXP_REPLACE(LOWER(title), '[0-9]+', '', 'g'),
-- Extract first number group and convert to integer for numeric sort -- Extract first number group and convert to integer for numeric sort
COALESCE( COALESCE(
NULLIF(REGEXP_REPLACE(LOWER(title), '^[^0-9]*', '', 'g'), '')::int, (REGEXP_MATCH(LOWER(title), '\d+'))[1]::int,
0 0
), ),
-- Then by full title as fallback -- Then by full title as fallback
@@ -253,7 +253,7 @@ pub async fn list_series(
PARTITION BY COALESCE(NULLIF(series, ''), 'unclassified') PARTITION BY COALESCE(NULLIF(series, ''), 'unclassified')
ORDER BY ORDER BY
REGEXP_REPLACE(LOWER(title), '[0-9]+', '', 'g'), 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 title ASC
) as rn ) as rn
FROM books FROM books
@@ -277,7 +277,7 @@ pub async fn list_series(
REGEXP_REPLACE(LOWER(sc.name), '[0-9]+', '', 'g'), REGEXP_REPLACE(LOWER(sc.name), '[0-9]+', '', 'g'),
-- Extract first number group and convert to integer -- Extract first number group and convert to integer
COALESCE( COALESCE(
NULLIF(REGEXP_REPLACE(LOWER(sc.name), '^[^0-9]*', '', 'g'), '')::int, (REGEXP_MATCH(LOWER(sc.name), '\d+'))[1]::int,
0 0
), ),
sc.name ASC sc.name ASC