---
title: orderBy (Window)
description: "Window\\.orderBy method in PySpark: Creates a WindowSpec with the ordering defined."
canonical_url: https://docs.databricks.com/gcp/en/pyspark/reference/classes/window/orderBy
---

# orderBy (Window)

Creates a `WindowSpec` with the ordering defined.

## Syntax

```
Window.orderBy(*cols)
```

## Parameters

| Parameter | Type                 | Description                      |
| --------- | -------------------- | -------------------------------- |
| `cols`    | str, Column, or list | Names of columns or expressions. |

## Returns

`WindowSpec`

## Examples

```python
from pyspark.sql import Window, functions as sf

df = spark.createDataFrame(
    [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"])

# Show row number ordered by category in each id partition.
window = Window.partitionBy("id").orderBy("category")
df.withColumn("row_number", sf.row_number().over(window)).show()
# +---+--------+----------+
# | id|category|row_number|
# +---+--------+----------+
# |  1|       a|         1|
# |  1|       a|         2|
# |  1|       b|         3|
# |  2|       a|         1|
# |  2|       b|         2|
# |  3|       b|         1|
# +---+--------+----------+
```
