Thread: array sort for varchar arrays?
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'}.
> 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'}. You've got it right. On Pg 8.3.3 I get SELECT sort(array['z','y','x']) -> {x,y,z}
2008/7/8 Matthew Dennis <mdennis@merfer.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
>> 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'}. > > You've got it right. On Pg 8.3.3 I get > SELECT sort(array['z','y','x']) -> {x,y,z} > Crap. There's a sort function included for int[]. I tried it on a varchar[] and it worked, but that was because I had made a function that was pretty much what pavel posted.
On Wed, Jul 9, 2008 at 12:28 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:
Thanks Pavel, I ended up writing a method pretty much like that. It seems like that would be inefficient though (works ok for my use though). Does anyone know if there are future plans to add an array_sort method?
2008/7/8 Matthew Dennis <mdennis@merfer.net>:> I'm likely overlooking something, but I can't seem to find a function tocreate function sort(anyarray)
> sort a varchar array. Something like "select sort('{y,z,x}'::varchar[])" =>
> {'x','y','z'}.
>
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
Thanks Pavel, I ended up writing a method pretty much like that. It seems like that would be inefficient though (works ok for my use though). Does anyone know if there are future plans to add an array_sort method?
2008/7/9 Matthew Dennis <mdennis@merfer.net>: > On Wed, Jul 9, 2008 at 12:28 AM, Pavel Stehule <pavel.stehule@gmail.com> > wrote: >> >> 2008/7/8 Matthew Dennis <mdennis@merfer.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 > > Thanks Pavel, I ended up writing a method pretty much like that. It seems > like that would be inefficient though (works ok for my use though). Does > anyone know if there are future plans to add an array_sort method? > Hello, I don't know about it. You should rewrite code from intarray contrib module and modify it for varchars. With varchars it could be little bit more work - maybe one day. Regards Pavel Stehule