msayag
msayag

Reputation: 8665

How to order items in tuples?

I have pairs of numbers and I want to sort them.

grunt> dump unordered
(11,22)
(88,33)
(55,66)

How do I sort them to: (11,22) (33,88) (55,66)

Tried to use bags: grunt> bag_of_pairs = foreach unordered generate TOBAG(TOTUPLE($0),TOTUPLE($1)); grunt> ordered = foreach bag_of_pairs {o1 = order $0 by $0; generate o1;}

And ended up with this ordered, but over-wrapped list, that I don't know how to simplify:

grunt> dump ordered
({(11),(22)})
({(33),(88)})
({(55),(66)})

Thanks

Upvotes: 1

Views: 48

Answers (1)

mr2ert
mr2ert

Reputation: 5186

You need a UDF to convert the bag to a tuple. However, since you only need to order two items this can also be done with a bincond.

ordered = FOREACH unordered GENERATE ($0<$1?$0:$1), ($0<$1?$1:$0) ;

NOTE: I can't test this right now, but this should also work.

ordered = FOREACH unordered GENERATE FLATTEN(($0<$1?($0,$1):($1,$0)) ;

Upvotes: 1

Related Questions