.sortByKey(...) transformation

The sortByKey(asc) transformation orders (key, value) RDD by key and returns an RDD in ascending or descending order. Look at the following code snippet:

# Takes the origin code and delays, remove header
# runs a group by origin code via reduceByKey()
# sorting by the key (origin code)
(
flights
.zipWithIndex()
.filter(lambda (row, idx): idx > 0)
.map(lambda (row, idx): row)
.map(lambda c: (c[3], int(c[1])))
.reduceByKey(lambda x, y: x + y)
.sortByKey()
.take(50)
)

This will produce this output:

# Output
[(u'ABE', 5113),
(u'ABI', 5128),
(u'ABQ', 64422),
(u'ABY', 1554),
(u'ACT', 392),
...]