Menu
CoddyTech

Sort Rows

Part of the Fundamentals section of Coddy's PySpark journey. Lesson 7 of 18.

A DataFrame has no guaranteed row order unless you request one. orderBy("name") sorts ascending. A descending column expression uses F.col("score").desc().

scores.orderBy(F.col("score").desc(), "name")

The example sorts highest score first, then name ascending when scores tie. A second key makes equal scores deterministic. In this exercise, the checker preserves your row order.

challenge icon

Challenge

Easy

Return all rows sorted by score descending. For tied scores, sort name ascending. Names are unique and neither column contains null.

Write transform(df) and return a Spark DataFrame. The checker supplies df with schema name string, score int. The session and F (PySpark functions) are already available. Do not print; the checker displays the returned rows. Return columns in this order: name, score.

Try it yourself

def transform(df):
    return df
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online Python compiler