# Ways to order a \`DataFrame\` by mixed order?

**URL:** <https://discourse.julialang.org/t/ways-to-order-a-dataframe-by-mixed-order/21952>\
**Category:** Data\
**Created:** [March 17, 2019, 3:56am UTC](https://discourse.julialang.org/t/ways-to-order-a-dataframe-by-mixed-order/21952 "2019-03-17T03:56:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [March 17, 2019, 3:56am UTC](https://discourse.julialang.org/t/ways-to-order-a-dataframe-by-mixed-order/21952/1 "2019-03-17T03:56:45Z")

</div>

I wanted to order a DataFrame by certain mixed order, e.g. i want to order a DataFrame by `a_str` in  
descending order followed by `b_str` in ascending order

**Setting up data**

```julia
using DataFrames, Query

a_str = string.(rand(UInt16, 100))
b_str = string.(rand(UInt16,100))

df = DataFrame(a_str = a_str, b_str = b_str)

```

**DataFrames.jl**

```julia
@> begin
  df
  sort!([order(:a_str, rev = true), :b_str])
end

```

\*\*DataFramesMeta.jl \*\*

_Not possible_. See: the DataFramesMeta.jl had [an outstanding issue from 2017](https://github.com/JuliaData/DataFramesMeta.jl/issues/81)

**Query.jl way**

```julia
#const Q = Query
@> begin
  df
  @orderby_descending _.a_str
  @thenby _.b_str
end

```

Query.jl has an implementation. But I think Query.jl used to have some significant performance issues.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [March 17, 2019, 4:18am UTC](https://discourse.julialang.org/t/ways-to-order-a-dataframe-by-mixed-order/21952/2 "2019-03-17T04:18:26Z")

</div>

A more natural way to write the [Query.jl](https://github.com/queryverse/Query.jl) version would be:

```julia
df |>
  @orderby_descending(_.a_str) |>
  @thenby(_.b_str) |> 
  DataFrame

```

You can just use the normal julia pipe operator.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [March 17, 2019, 4:26am UTC](https://discourse.julialang.org/t/ways-to-order-a-dataframe-by-mixed-order/21952/3 "2019-03-17T04:26:52Z")

</div>

I like not having to type `|>`. In Rstudio `%>%` is mapped to `Ctrl-Shift-m` which is more convenient.
