Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is string array

SQL
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

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


How i can do this?
Posted

1 solution

I'm not sure, but it would be something like that:
SQL
SELECT *
FROM (
     SELECT unnest(ARRAY['CAT','CAT DOG CAT','DOG Cat']) AS Aminals, unnest(ARRAY[3,1,2]) AS SortOrder
     ) AS foo
ORDER BY SortOrder


See here:
8.14. Arrays[^]
9.17. Array Functions and Operators[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900