# Sorting a DataFrame based on a multi-column function

**URL:** <https://discourse.julialang.org/t/sorting-a-dataframe-based-on-a-multi-column-function/122301>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 5, 2024, 5:51pm UTC](https://discourse.julialang.org/t/sorting-a-dataframe-based-on-a-multi-column-function/122301 "2024-11-05T17:51:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alancummings](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alancummings/32/46788_2.png) [@alancummings](https://discourse.julialang.org/u/alancummings)\
**Post date:** [November 5, 2024, 5:51pm UTC](https://discourse.julialang.org/t/sorting-a-dataframe-based-on-a-multi-column-function/122301/1 "2024-11-05T17:51:41Z")

</div>

Is there an easier way to do this?  
`sort(transform(df, [:b, :c] => ByRow(max) => : __max__ ), : __max__ , rev=true)[:, names(df)]`

I want to sort based on a function of more than one column (in this case the max of the selected columns), hopefully without adding an extra column.

Example df:  
`df = DataFrame(a=["d", "c", "b", "a"], b=[1,2,3,4], c=[-2, 5, 1,2])`

I tried these, but they did not work:  
`sort(df, [:b, :c], by=maximum)`  
`sort(df; by=row -> max(row...))`  
`sort(df; by=row -> max(row.b, row.c))` ← Google AI told me this would work. Google AI was wrong  
`sort(df, [:b, :c]; by=ByRow((b,c) -> max(b,c)))`  
`sort(df, [:b, :c]; by=(b,c) -> max(b,c))`

Is there any way sort based on a function of multiple columns? (other than my workaround at the top)

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [November 5, 2024, 6:17pm UTC](https://discourse.julialang.org/t/sorting-a-dataframe-based-on-a-multi-column-function/122301/2 "2024-11-05T18:17:42Z")

</div>

From what I understand, `sort` considers columns as independent when sorting and does not allow the type of combination you want.  
Instead, you could just use indexing to a similar effect:

```julia
df[sortperm(max.(df.b, df.c); rev=true), names(df)]

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 5, 2024, 6:51pm UTC](https://discourse.julialang.org/t/sorting-a-dataframe-based-on-a-multi-column-function/122301/3 "2024-11-05T18:51:09Z")

</div>

> [@bertschi](#):
>
> `df[sortperm(max.(df.b, df.c); rev=true), names(df)]`

Or even

```julia
df[sortperm(max.(df.b, df.c); rev=true), :]

```

😉

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 5, 2024, 8:49pm UTC](https://discourse.julialang.org/t/sorting-a-dataframe-based-on-a-multi-column-function/122301/4 "2024-11-05T20:49:58Z")

</div>

In DataFramesMeta.jl this is

```julia
@orderby df max.(:b, :c)

```
