Spark: Sort an Rdd by Multiple Values in a Tuple / Columns

So I have an RDD as follows

RDD[(String, Int, String)]

And as an example

    ('b', 1, 'a')
    ('a', 1, 'b')
    ('a', 0, 'b')
    ('a', 0, 'a')

The final result should look something like

('a', 0, 'a')
('a', 0, 'b')
('a', 1, 'b')
('b', 1, 'a')

How would I do something like this?

1 Answer

Try this:

rdd.sortBy(r => r)

If you wanted to switch the sort order around, you could do this:

rdd.sortBy(r => (r._3, r._1, r._2))

For reverse order:

rdd.sortBy(r => r, false)
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest