Skip to content

Instantly share code, notes, and snippets.

View jemminger's full-sized avatar

Jeff Emminger jemminger

  • Seventh Compass
  • Florida
View GitHub Profile
@jemminger
jemminger / psql_naturalsort
Created October 24, 2018 16:33 — forked from veob/psql_naturalsort
PostgreSQL natural sort
//sql
create or replace function naturalsort(text)
returns bytea language sql immutable strict as
$f$ select string_agg(convert_to(coalesce(r[2],length(length(r[1])::text) || length(r[1])::text || r[1]),'SQL_ASCII'),'\x00')
from regexp_matches($1, '0*([0-9]+)|([^0-9]+)', 'g') r; $f$;
//author: github.com/RhodiumToad