# DataFrames and sorting - is the default sort a stable sort?

**URL:** https://discourse.julialang.org/t/dataframes-and-sorting-is-the-default-sort-a-stable-sort/122297
**Category:** New to Julia
**Tags:** question
**Created:** [November 5, 2024, 4:16pm UTC](https://discourse.julialang.org/t/dataframes-and-sorting-is-the-default-sort-a-stable-sort/122297 "2024-11-05T16:16:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)
#### Post date: [November 5, 2024, 4:16pm UTC](https://discourse.julialang.org/t/dataframes-and-sorting-is-the-default-sort-a-stable-sort/122297/1 "2024-11-05T16:16:53Z")

</div>

Here’s a link to the documentation page on `DataFrames` and sorting.

- [Sorting · DataFrames.jl](https://dataframes.juliadata.org/stable/man/sorting/)

Some details are provided here, but unfortunately the information as to whether the default sorting algorithm is a stable sort is not included.

Does anyone happen to know if `DataFrames` uses a stable sort?

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [November 5, 2024, 4:33pm UTC](https://discourse.julialang.org/t/dataframes-and-sorting-is-the-default-sort-a-stable-sort/122297/2 "2024-11-05T16:33:07Z")

</div>

Afaiu it uses a stable sort by default, cf [here](https://github.com/JuliaData/DataFrames.jl/blob/85815e45c559aae14a194f913e25df0e484248bd/src/abstractdataframe/sort.jl#L314):

```julia
Sort.defalg(df::AbstractDataFrame) =
    size(df, 1) < 8192 ? Sort.MergeSort : SortingAlgorithms.TimSort

```

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [November 5, 2024, 5:54pm UTC](https://discourse.julialang.org/t/dataframes-and-sorting-is-the-default-sort-a-stable-sort/122297/3 "2024-11-05T17:54:07Z")

</div>

This could be documented better (there)! And _probably_ the implementation should be changed to just use Julia’s new default sort implementation.

I would have assumed for such works stable sorting is always used, but actually for Pandas I see not, unless I’m misreading its docs (though stable is an option there):

```julia
DataFrame.sort_values(*by*, ***, *axis=0*, *ascending=True*, *inplace=False*, 

```

> [@world-peace](#):
>
> Does anyone happen to know if `DataFrames` uses a stable sort?

Note, Julia uses stable sort _by default_, so maybe all docs of packages should assume that, unless saying otherwise.

@foobar_lv2, I see lines preceding:

```julia
# TimSort is fast for data with structure, but only if the DataFrame is large enough
# TODO: 8192 is informed but somewhat arbitrary

```

If TimSort were fast/est then Julia would use be default? It’s Pandas’ default stable sort, and mergesort there an alias for it.

> help?\> sort!  
> sort!(v; alg::Algorithm=defalg(v), lt=isless, by=identity, rev::Bool=false, order::Ordering=Forward)
> 
> Sort the vector v in place. A stable algorithm is used by default: the ordering of elements that compare equal is preserved. […]

> help?\> ?Sort.DEFAULT\_STABLE  
> The default sorting algorithm.
> 
> This algorithm is guaranteed to be stable (i.e. it will not reorder elements that compare equal). It makes an effort to be fast for most inputs.
> 
> The algorithms used by DEFAULT\_STABLE are an implementation detail. See extended help for the current dispatch system.
> 
> Extended Help  
> ≡≡≡≡≡≡≡≡≡≡≡≡≡
> 
> DEFAULT\_STABLE is composed of two parts: the InitialOptimizations and a hybrid of Radix, Insertion, Counting, Quick sorts.
> 
> [I count probably a hybrid of 6 algorithms currently used, I know a lot of work went into this in recent times]
> 
> Finally, if the input has length less than 80, we dispatch to InsertionSort and otherwise we dispatch to ScratchQuickSort.

> help?\> ?Sort.DEFAULT\_UNSTABLE  
> …  
> The algorithms used by DEFAULT\_UNSTABLE are an implementation detail. **They are currently the same as those used by DEFAULT\_STABLE,** but this is subject to change in future.

[It used to mean the unstable QuickSort.]

Do not ask for this directly (there’s not need for since now slower I think, or at least no speed-improvement over the stable algorithm):

```julia
help?> Sort.QuickSort
  QuickSort

  Indicate that a sorting function should use the quick sort algorithm, **which is not stable.** [,,[

```

Julia has always sorted stably be default (or am I reading it wrong in old 1.6 docs):

> Sort the vector v in place. QuickSort is used by default for numeric arrays while MergeSort is used for other arrays.
