25

Running this query:

select name from folders order by name

returns these results:

alphanumeric
a test
test 20
test 19
test 1
test 10

But I expected:

a test
alphanumeric
test 1
test 10
test 19
test 20

What's wrong here?

8
  • 1
    It looks weird: 'test 20' < 'test 19'. What LC_COLLATE you have by examinating SHOW lc_collate; ? I have en_US.UTF-8 and it returns exactly wanted output with ORDER BY name ASC. Commented Aug 10, 2011 at 22:32
  • I get the same thing, too. I just did select 'alphanumeric' < 'a test' and got f. Commented Aug 10, 2011 at 22:34
  • 1
    for the record, i posted an answer pointing you to the manual entry for collation. it was donwvoted as not being huggy-lovey enough so i deleted it. but i think you should start there. Commented Aug 10, 2011 at 22:37
  • @andrew cooke: I didn't downvote, that was good point, collation is responsible for ordering, however per-column collation, which could be useful here is supported only since Postgres 9.1. Commented Aug 10, 2011 at 22:41
  • Hello, thk for your comments... SHOW lc_collate; is returning es_SV.UTF-8 (I live in El Salvador) Commented Aug 10, 2011 at 22:44

7 Answers 7

29

You can simply cast name column to bytea data type allowing collate-agnostic ordering:

SELECT name
FROM folders
ORDER BY name::bytea;

Result:

     name     
--------------
 a test
 alphanumeric
 test 1
 test 10
 test 19
 test 20
(6 rows)
Sign up to request clarification or add additional context in comments.

2 Comments

This fixes sorting with leading spaces that TRIM would not solve.
Be careful, as it's still alphanumeric (meaning "3" will come after "20")
24

All of this methods sorted my selection in alphabetical order:

test 1
test 10
test 2
test 20

This solution worked for me (lc_collate: 'ru_RU.UTF8'):

SELECT name
FROM folders
ORDER BY SUBSTRING(name FROM '([0-9]+)')::BIGINT ASC, name;

test 1
test 2
test 10
test 20

Comments

15
select * from "public"."directory" where "directoryId" = 17888 order by
COALESCE(SUBSTRING("name" FROM '^(\d+)')::INTEGER, 99999999),
SUBSTRING("name" FROM '[a-zA-z_-]+'),
COALESCE(SUBSTRING("name" FROM '(\d+)$')::INTEGER, 0),
"name";

NOTE: Escape the regex as you need, in some languages, you will have to add one more "\".

In my Postgres DB, name column contains following, when I use simple order by name query:

  • 1
  • 10
  • 2
  • 21
  • A
  • A1
  • A11
  • A5
  • B
  • B2
  • B22
  • B3
  • M 1
  • M 11
  • M 2

Result of Query, After I have modified it:

  • 1
  • 2
  • 10
  • 21
  • A
  • A1
  • A5
  • A11
  • B
  • B2
  • B3
  • B22
  • M 1
  • M 2
  • M 11

Comments

8

You may be able to manually sort by splitting the text up in case there is trailing numerals, like so:

SELECT * FROM sort_test
ORDER BY SUBSTRING(text FROM '^(.*?)( \\d+)?$'),
         COALESCE(SUBSTRING(text FROM ' (\\d+)$')::INTEGER, 0);

This will sort on column text, first by all characters optionally excluding an ending space followed by digits, then by those optional digits.

Worked well in my test.

Update fixed the string-only sorting with a simple coalesce (duh).

4 Comments

Why the downvote? It works, and solves the situation. It's not the best solution, but it doesn't involve changing the database structure. At least comment if you feel the need to downvote.
-1 for bizarre kluge that doesn't solve the real problem. (What about strings with multiple spaces and/or numbers?) See the comments above about collations.
I wouldn't say it's a bizarre kludge. It adds the ability to sort trailing numbers numerically, without requiring a specific version of PG. It handles trailing numbers very well, so works well for sequentially numbered folders. It handles multiple spaces, because it's only checking to ensure that there is at least one space before the trailing digits. If you tried it, you'd see it worked, instead of assuming.
Not working for texts like: "H1C11", because it order for the first number, not for the tailing
5

OverZealous answer helped me but didn't work if the string in the database begun with numbers followed by additional characters.

The following worked for me:

SELECT name
FROM folders
ORDER BY
COALESCE(SUBSTRING(name FROM '^(\\d+)')::INTEGER, 99999999),
SUBSTRING(name FROM '^\\d* *(.*?)( \\d+)?$'),
COALESCE(SUBSTRING(name FROM ' (\\d+)$')::INTEGER, 0),
name;

So this one:

  1. Extracts the first number in the string, or uses 99999999.
  2. Extracts the string that follows the possible first number.
  3. Extracts a trailing number, or uses 0.

Comments

3

A Vlk's answer above helped me a lot, but it sorted items only by the numeric part, which in my case came second. My data was like (desk 1, desk 2, desk 3 ...) a string part, a space and a numeric part. The syntax in A Vlk's answer returned the data sorted by the number, and at that it was the only answer from the above that did the trick. However when the string part was different, (eg desk 3, desk 4, table 1, desk 5...) table 1 would get first from desk 2. I fixed this using the syntax below:

    ...order by SUBSTRING(name,'\\w+'), SUBSTRINGname FROM '([0-9]+)')::BIGINT ASC;

Comments

1

Tor's last SQL worked for me. However if you are calling this code from php you need add extra slashes.

SELECT name
FROM folders
ORDER BY
COALESCE(SUBSTRING(name FROM '^(\\\\d+)')::INTEGER, 99999999),
SUBSTRING(name FROM '^\\\\d* *(.*?)( \\\\d+)?$'),
COALESCE(SUBSTRING(name FROM ' (\\\\d+)$')::INTEGER, 0),
name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.