# How does @orderby work with multiple columns?

**URL:** <https://discourse.julialang.org/t/how-does-orderby-work-with-multiple-columns/88155>\
**Category:** Data\
**Tags:** question, dataframesmeta\
**Created:** [October 3, 2022, 1:02pm UTC](https://discourse.julialang.org/t/how-does-orderby-work-with-multiple-columns/88155 "2022-10-03T13:02:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![huguesmp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huguesmp/32/202444_2.png) [@huguesmp](https://discourse.julialang.org/u/huguesmp)\
**Post date:** [October 3, 2022, 1:02pm UTC](https://discourse.julialang.org/t/how-does-orderby-work-with-multiple-columns/88155/1 "2022-10-03T13:02:49Z")

</div>

Hello everyone,

I’m a new Julia user coming from an R/dplyr background.

I’m going through the “[Tutorial for coming from dplyr](https://juliadata.github.io/DataFramesMeta.jl/stable/dplyr/#Arrange-Or-Re-order-Rows-Using-@orderby)” on the DataFramesMeta.jl documentation site, specifically the `@orderby` section using `Chain.jl`.

I modified one of the examples to get a feel for things. I was expecting the column `:sleep_total` to be in reverse order for a particular `:order`, but this isn’t what I’m seeing here:

```julia
julia> @chain msleep begin
           @select :name :order :sleep_total
           @orderby begin
               :order
               sortperm(:sleep_total, rev=true)
           end
       end
83×3 DataFrame
 Row │ name order sleep_total
     │ String31 String15 Float64
─────┼───────────────────────────────────────────────────────────
   1 │ Tenrec Afrosoricida 15.6
   2 │ Pig Artiodactyla 9.1
   3 │ Cow Artiodactyla 4.0
   4 │ Roe deer Artiodactyla 3.0
   5 │ Sheep Artiodactyla 3.8
   6 │ Goat Artiodactyla 5.3
   7 │ Giraffe Artiodactyla 1.9
   8 │ Northern fur seal Carnivora 8.7
   9 │ Tiger Carnivora 15.8
  10 │ Lion Carnivora 13.5
  11 │ Genet Carnivora 6.3
  12 │ Domestic cat Carnivora 12.5
  13 │ Red fox Carnivora 9.8
  14 │ Arctic fox Carnivora 12.5
  15 │ Cheetah Carnivora 12.1
  16 │ Caspian seal Carnivora 3.5
  17 │ Jaguar Carnivora 10.4
  18 │ Gray seal Carnivora 6.2

```

For example, the `:order` “Artiodactyla” has its `:sleep_total` values in no particular order, neither ascending nor decending. I tried a couple syntax variants after looking at `@orderby` documentation, but I get the same result.

I hope I’m not missing anything obvious…

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [October 3, 2022, 3:58pm UTC](https://discourse.julialang.org/t/how-does-orderby-work-with-multiple-columns/88155/2 "2022-10-03T15:58:47Z")

</div>

The example in the docsting is misleading. You should not use `sortperm` in such situations as it does not make much sense IMHO. Instead use e.g. `ordinalrank` from StatsBase.jl or just negation in your case (as your column is continuous):

```julia
using StatsBase
@chain msleep begin
    @select :name :order :sleep_total
    @orderby begin
        :order
        ordinalrank(:sleep_total, rev=true)
    end
end
@chain msleep begin
    @select :name :order :sleep_total
    @orderby :order -:sleep_total
end

```

---

<div class="post-metadata">

**Author:** ![huguesmp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huguesmp/32/202444_2.png) [@huguesmp](https://discourse.julialang.org/u/huguesmp)\
**Post date:** [October 3, 2022, 4:08pm UTC](https://discourse.julialang.org/t/how-does-orderby-work-with-multiple-columns/88155/3 "2022-10-03T16:08:31Z")

</div>

I feel a bit silly for not trying `-:sleep_total`, but both examples produce the output I was expecting.

Thank you!

---

<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:** [October 3, 2022, 5:08pm UTC](https://discourse.julialang.org/t/how-does-orderby-work-with-multiple-columns/88155/4 "2022-10-03T17:08:26Z")

</div>

Apologies for this. We will get this fixed soon.
