版权声明:本文为博主原创文章,转载请注明【转载自皓月如我的CSDN博客】,并贴出原始链接地址。 https://blog.csdn.net/fm0517/article/details/88072441
create or replace function RTU_ARRAY_SORT(anyarray text[],f_order text) returns text[]
as declare
tmp text;
result text[];
begin
if lower(f_order) = 'desc' then
for tmp in select unnest(anyarray) as a order by a desc
loop
result := array_append(result,tmp::text);
end loop;
return result;
elsif lower(f_order) = 'asc' then
for tmp in select unnest(anyarray) as a order by a asc
loop
result := array_append(result,tmp::text);
end loop;
return result;
else
return array['f_order must be asc or desc!'];
end if;
end;
select RTU_ARRAY_SORT('{1,3,2}','asc');
select RTU_ARRAY_SORT('{1,3,2}','desc');
select RTU_ARRAY_SORT('{a,c,b}','asc');
select RTU_ARRAY_SORT('{a,c,b}','desc');
返回分别是:
{1,2,3}
{3,2,1}
{a,b,c}
{c,b,a}