Re: Alphanumeric natural order sorting

From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Umashanker, Srividhya *EXTERN*" <srividhya(dot)umashanker(at)hp(dot)com>, "pgeu-general(at)postgresql(dot)org" <pgeu-general(at)postgresql(dot)org>
Subject: Re: Alphanumeric natural order sorting
Date: 2013-03-20 08:13:17
Message-ID: A737B7A37273E048B164557ADEF4A58B057C98F2@ntex2010a.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgeu-general pgsql-general

Umashanker, Srividhya wrote:
> I am looking for a solution the Alphanumeric sorting
>
> I tried all possible collation example -- select * from test order by name collate "en_US";
>
> Is there someone who has solved this problem with writing a function?

> I am expecting
>
> 1, bay1
> 2, bay2
> 10, bay10
> 11, bay11

That is not the correct sort order in any collation,
because either '1' < '2' or '1' > '2' (assuming that '1' <> '2').

You can pick out the parts with a regular expression:
SELECT * FROM test
ORDER BY regexp_replace(name, '^([^[:digit:]]*).*$', '\1'),
regexp_replace(name, '^.*?([[:digit:]]*)$', '\1')::bigint;

Or you split the column into two columns, one a string and
the other a number, and use these for sorting.

Yours,
Laurenz Albe

In response to

Responses

Browse pgeu-general by date

  From Date Subject
Next Message Umashanker, Srividhya 2013-03-20 08:18:30 Re: Alphanumeric natural order sorting
Previous Message Hans-Jürgen Schönig 2013-03-20 07:33:13 Re: Case insensitve sort

Browse pgsql-general by date

  From Date Subject
Next Message Umashanker, Srividhya 2013-03-20 08:18:30 Re: Alphanumeric natural order sorting
Previous Message Umashanker, Srividhya 2013-03-20 06:34:05 Alphanumeric natural order sorting