# reorder

> For the complete documentation index, see [llms.txt](https://kotlin.github.io/dataframe/llms.txt).

Changes the order of the selected columns. `reorder` selects the columns; finish it with `by`, `byDesc` or `byName` to get a new [DataFrame](dataframe.html).

API reference: [reorder](https://kotlin.github.io/dataframe/api-docs/core/org.jetbrains.kotlinx.dataframe.api/reorder.html)

```TEXT
reorder { columns }
  [.cast<ColumnType>() ]
   .by { columnExpression } | .byDesc { columnExpression } | .byName(desc = false)
    
columnExpression: DataColumn.(DataColumn) -> Value
```

Related operations: [Move / rename / reorder columns](moverename.html)

Don't confuse `reorder` with [sortBy](sortby.html), which changes the order of rows, not columns.

See [column selectors](columnselectors.html) for how to select the columns for this operation.

The examples on this page use the following dataframe:

```KOTLIN
df
```

[Embedded content](./resources/reorderDf.html)

Properties:

```KOTLIN
df.reorder { age..isHappy }.byName()
```

Strings:

```KOTLIN
df.reorder { "age".."isHappy" }.byName()
```

[Embedded content](./resources/reorder.html)

The selected columns change places only with each other: they take the positions of the selected columns, in the new order. Positions of other columns do not change. The values in the columns do not change either.

The `columnExpression` lambda is computed for every selected column. The column is passed to it both as the receiver (`this`) and as the argument (`it`).

`by` puts the columns in ascending order of the computed value:

```KOTLIN
val df = dataFrameOf("c", "d", "a", "b")(
    3, 4, 1, 2,
    1, 1, 1, 1,
)
df.reorder("d", "b").cast<Int>().by { sum() }
```

[Embedded content](./resources/reorderSome.html)

`byDesc` puts them in descending order:

```KOTLIN
val df = dataFrameOf("c", "d", "a", "b")(
    3, 4, 1, 2,
    1, 1, 1, 1,
)
df.reorder("a", "b").cast<Int>().byDesc { sum() }
```

[Embedded content](./resources/reorderByDesc.html)

Columns for which `columnExpression` gives equal results keep their original order. Here `name` and `city` have names of the same length, so `name` stays before `city`:

Properties:

```KOTLIN
df.reorder { age and name and city }.byDesc { it.name().length }
```

Strings:

```KOTLIN
df.reorder("age", "name", "city").byDesc { it.name().length }
```

[Embedded content](./resources/reorderEqualKeys.html)

If selected columns belong to different column groups, they are reordered within their groups, so no column moves to another group. Here `age` and `city` change places at the top level, and `firstName` and `lastName` change places inside `name`:

Properties:

```KOTLIN
df.reorder { age and city and name.firstName and name.lastName }.byName(desc = true)
```

Strings:

```KOTLIN
df.reorder { "age" and "city" and "name"["firstName"] and "name"["lastName"] }.byName(desc = true)
```

[Embedded content](./resources/reorderInDifferentGroups.html)

When exactly one [ColumnGroup](datacolumn.html#columngroup) is selected, reordering is applied to its nested columns. A column group selected together with other columns moves as a whole, and its nested columns keep their order.

Properties:

```KOTLIN
df.reorder { name }.byName(desc = true)
```

Strings:

```KOTLIN
df.reorder("name").byName(desc = true)
```

[Embedded content](./resources/reorderInGroup.html)

## reorderColumnsBy

Reorders all columns of the dataframe by the value of `columnExpression`.

```TEXT
reorderColumnsBy(atAnyDepth = true, desc = false, inFrameColumns = false) { columnExpression }
```

Parameters:

* `atAnyDepth` — if `true`, also reorder the columns inside every [ColumnGroup](datacolumn.html#columngroup), each within its own group. If `false`, only the top-level columns are reordered.

* `desc` — apply descending order

* `inFrameColumns` — if `true`, also reorder the columns inside every dataframe of a [FrameColumn](datacolumn.html#framecolumn), each dataframe on its own and with the same `atAnyDepth`. If `false`, they keep their order: like [colsAtAnyDepth()](columnselectors.html#cols-at-any-depth), `atAnyDepth` does not look inside frame columns.

As in `reorder`, columns for which `columnExpression` gives equal results keep their original order. The examples use the dataframe from the top of the page. With the default `atAnyDepth = true`, the columns inside `name` are reordered too:

```KOTLIN
df.reorderColumnsBy { name().length }
```

[Embedded content](./resources/reorderColumnsBy.html)

With `atAnyDepth = false`, the columns inside `name` keep their order:

```KOTLIN
df.reorderColumnsBy(atAnyDepth = false) { name().length }
```

[Embedded content](./resources/reorderColumnsByTopLevel.html)

## reorderColumnsByName

Reorders all columns of the dataframe in lexicographic order of their names. It is a shortcut for `reorderColumnsBy { name() }`.

```TEXT
reorderColumnsByName(atAnyDepth = true, desc = false, inFrameColumns = false)
```

Parameters:

* `atAnyDepth` — if `true`, also reorder the columns inside every [ColumnGroup](datacolumn.html#columngroup), each within its own group. If `false`, only the top-level columns are reordered.

* `desc` — apply descending order

* `inFrameColumns` — if `true`, also reorder the columns inside every dataframe of a [FrameColumn](datacolumn.html#framecolumn), each dataframe on its own and with the same `atAnyDepth`. If `false`, they keep their order.

The examples use the dataframe from the top of the page. With the default `atAnyDepth = true`, the columns inside `name` are reordered too:

```KOTLIN
df.reorderColumnsByName(desc = true)
```

[Embedded content](./resources/reorderColumnsByName.html)

With `atAnyDepth = false`, the columns inside `name` keep their order:

```KOTLIN
df.reorderColumnsByName(atAnyDepth = false, desc = true)
```

[Embedded content](./resources/reorderColumnsByNameTopLevel.html)

