2

In hive, I wish to sort an array from largest to smallest, and get the index array.

For example, the table is like this:

id  |  value_array
 1  |  {30, 40, 10, 20}
 2  |  {10, 30, 40, 20}

I with to get this:

id  |  value_array
 1  |  {1, 0, 3, 2}
 2  |  {2, 1, 3, 0}

The arries in result are the index of the initial elements. How can I achieve this?

0

1 Answer 1

1

Explode array using posexplode to get index and value, sort by value, collect array of index:

select id, collect_list(pos) as result_array
from
(
select s.id, a.pos, a.v 
  from your_table s
       lateral view posexplode(s.value_array) a as pos, v
distribute by s.id sort by a.v DESC --sort by value
)s
group by id
;

Tested, result:

id  result_array
1   [1,0,3,2]
2   [2,1,3,0]
Sign up to request clarification or add additional context in comments.

2 Comments

@VamsiPrabhala "from largest to smallest" 2->40, 1->30, and so on
@VamsiPrabhala okay, np

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.