Re: array sort for varchar arrays?

From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "Matthew Dennis" <mdennis(at)merfer(dot)net>
Cc: PGSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: array sort for varchar arrays?
Date: 2008-07-09 05:28:22
Message-ID: 162867790807082228o14d5f10aoca6629025d6b3255@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2008/7/8 Matthew Dennis <mdennis(at)merfer(dot)net>:
> I'm likely overlooking something, but I can't seem to find a function to
> sort a varchar array. Something like "select sort('{y,z,x}'::varchar[])" =>
> {'x','y','z'}.
>

create function sort(anyarray)
returns anyarray as $$
select array(select $1[i] from generate_series(array_lower($1,1),
array_upper($1,1)) g(i) order by 1)
$$ language sql strict immutable;

postgres=# select sort(array[1,2,3,1]);
sort
-----------
{1,1,2,3}
(1 row)

postgres=# select sort(array['a','b','c','a']);
sort
-----------
{a,a,b,c}
(1 row)

Regards
Pavel Stehule

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tino Wildenhain 2008-07-09 05:58:30 Re: Getting source code for database objects
Previous Message Tom Lane 2008-07-09 04:48:34 Re: Feature: FOR UPDATE SKIP LOCKED