Re: Alphanumeric natural order sorting

From: "Umashanker, Srividhya" <srividhya(dot)umashanker(at)hp(dot)com>
To: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>, "pgeu-general(at)postgresql(dot)org" <pgeu-general(at)postgresql(dot)org>
Subject: Re: Alphanumeric natural order sorting
Date: 2013-03-20 08:18:30
Message-ID: 5D5078F9198B04449B9D74EA9E7F3D580CD24A1E@G1W3640.americas.hpqcorp.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgeu-general pgsql-general

We are working on a framework, where the client can

* call for sort on any colmn.

* The digits may or may not be there

* The numeric can be anywhere in the string

-vidhya

-----Original Message-----
From: Albe Laurenz [mailto:laurenz(dot)albe(at)wien(dot)gv(dot)at]
Sent: Wednesday, March 20, 2013 1:43 PM
To: Umashanker, Srividhya; pgeu-general(at)postgresql(dot)org
Subject: RE: Alphanumeric natural order sorting

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 Albe Laurenz 2013-03-20 08:46:49 Re: Alphanumeric natural order sorting
Previous Message Albe Laurenz 2013-03-20 08:13:17 Re: Alphanumeric natural order sorting

Browse pgsql-general by date

  From Date Subject
Next Message Albe Laurenz 2013-03-20 08:46:49 Re: Alphanumeric natural order sorting
Previous Message Albe Laurenz 2013-03-20 08:13:17 Re: Alphanumeric natural order sorting