3

There is string array

ARRAY['CAT','CAT DOG CAT','DOG Cat']

Now i want to sort this array on count of words in each element. I have tried but cannot get any success.

I want this output

ARRAY['CAT DOG CAT','DOG CAT','Cat']

How i can do this?

4

2 回答 2

1

这确实感觉很笨拙,但我现在想不出更简单的解决方案:

with val (col) as (
  values (ARRAY['CAT','CAT DOG CAT','DOG Cat'])
), word_list as (
  select unnest(col) as pc
  from val
), wc as (
  select array_length(string_to_array(pc, ' '),1) as word_count, pc
  from word_list
)
select array_agg(pc order by word_count desc)
from wc;
于 2013-08-01T10:38:15.830 回答
1

我想出了一个替代方案:

select array_agg(a) 
from (
  select unnest(array['CAT','CAT DOG CAT','DOG Cat']) as a 
  order by a) s
于 2020-10-30T19:12:30.760 回答