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}
create or replace function ARRAY_SORT(anyarray int[],f_order text) returns int[]
as declare
tmp int;
result int[];
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);
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);
end loop;
return result;
else
return array['f_order must be asc or desc!'];
end if;
end;
select ARRAY_SORT(array[1,3,2],'asc');