如何对包含由句点分隔符分隔的字母数字字符的文本字段进行排序?
How to sort the text field that contains alpha numeric characters separated by period delimiter?
我需要以自然排序方式对字段进行排序,但无法实现。有办法实现吗?
这是用于 PostgreSQL 代码修复并尝试了各种代码片段,但其中 none returns 行符合预期
-- PostgresSQL 9.5 代码
SELECT name
FROM (
SELECT lower('1.1.1.a.1') as name union all
SELECT lower('1.1.a.1') as name union all
SELECT lower('1.1.1.a.11') as name
) folders
ORDER BY
COALESCE(SUBSTRING(name FROM '^(\d+)')::INTEGER, 99999999),
SUBSTRING(name FROM '^\d* *(.*?)( \d+)?$'),
COALESCE(SUBSTRING(name FROM ' (\d+)$')::INTEGER, 0),
name;
我希望输出按以下顺序排序
Expected Output:
================
1.1.a.1
1.1.1.a.1
1.1.1.a.11
Actual (current) Output:
========================
1.1.1.a.1
1.1.1.a.11
1.1.a.1
您可以先按长度排序,然后按从元素创建的数组排序:
SELECT name
FROM (
values
('1.1.1.a.1'), ('1.1.a.1'), ('1.1.1.a.11'), ('1.1.1.a.2')
) folders (name)
ORDER BY length(name), string_to_array(name, '.');
returns
name
----------
1.1.a.1
1.1.1.a.1
1.1.1.a.2
1.1.1.a.11
不确定这是否涵盖所有情况
我需要以自然排序方式对字段进行排序,但无法实现。有办法实现吗?
这是用于 PostgreSQL 代码修复并尝试了各种代码片段,但其中 none returns 行符合预期
-- PostgresSQL 9.5 代码
SELECT name
FROM (
SELECT lower('1.1.1.a.1') as name union all
SELECT lower('1.1.a.1') as name union all
SELECT lower('1.1.1.a.11') as name
) folders
ORDER BY
COALESCE(SUBSTRING(name FROM '^(\d+)')::INTEGER, 99999999),
SUBSTRING(name FROM '^\d* *(.*?)( \d+)?$'),
COALESCE(SUBSTRING(name FROM ' (\d+)$')::INTEGER, 0),
name;
我希望输出按以下顺序排序
Expected Output:
================
1.1.a.1
1.1.1.a.1
1.1.1.a.11
Actual (current) Output:
========================
1.1.1.a.1
1.1.1.a.11
1.1.a.1
您可以先按长度排序,然后按从元素创建的数组排序:
SELECT name
FROM (
values
('1.1.1.a.1'), ('1.1.a.1'), ('1.1.1.a.11'), ('1.1.1.a.2')
) folders (name)
ORDER BY length(name), string_to_array(name, '.');
returns
name
----------
1.1.a.1
1.1.1.a.1
1.1.1.a.2
1.1.1.a.11
不确定这是否涵盖所有情况