# \[ANN\] SortingLab.jl - fast sorting algorithms for strings and CategoricalArrays

**URL:** https://discourse.julialang.org/t/ann-sortinglab-jl-fast-sorting-algorithms-for-strings-and-categoricalarrays/19653
**Category:** Package Announcements
**Tags:** sort, sortperm
**Created:** [January 15, 2019, 11:05am UTC](https://discourse.julialang.org/t/ann-sortinglab-jl-fast-sorting-algorithms-for-strings-and-categoricalarrays/19653 "2019-01-15T11:05:09Z")
**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: [January 15, 2019, 11:05am UTC](https://discourse.julialang.org/t/ann-sortinglab-jl-fast-sorting-algorithms-for-strings-and-categoricalarrays/19653/1 "2019-01-15T11:05:09Z")

</div>

> **[GitHub - xiaodaigh/SortingLab.jl: Faster sorting algorithms (sort and sortperm) for...](https://github.com/xiaodaigh/SortingLab.jl)**
>
> Faster sorting algorithms (sort and sortperm) for Julia

I have updated the package to support Julia v1. There is no-intention of supporting Julia v0.6.

## Benchmarks

 ![sort_vs_radixsort](https://global.discourse-cdn.com/julialang/original/3X/4/f/4fae1af84d3350fedd9a16e0d954173c7f1ec5eb.png)  
 ![sortperm_vs_fsortperm](https://global.discourse-cdn.com/julialang/original/3X/8/2/8209721d0dba36afd9cf718c00933b5d9e74856a.png)

## Example usage

```julia-auto
using SortingLab;
import Test: @test

N = 1_000_000;
K = 100;

# faster string sort
svec = rand("id".*string.(1:N÷K, pad=10), N);
svec_sorted = radixsort(svec);
issorted(svec_sorted) # true
issorted(svec) # false

# faster string sortperm
sorted_idx = fsortperm(svec)
issorted(svec[sorted_idx]) #true

# in place string sort
radixsort!(svec);
issorted(svec) # true

# CategoricalArray sort
using CategoricalArrays
pools = "id".*string.(1:100,3);
byvec = CategoricalArray{String, 1}(rand(UInt32(1):UInt32(length(pools)), N), CategoricalPool(pools, false));
byvec = compress(byvec);

byvec_sorted = fsort(byvec);
@test issorted(byvec_sorted)

# in place CategoricalArray sort
fsort!(byvec)
@test issorted(byvec)

```

---

<div class="post-metadata">

### Author: ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)
#### Post date: [January 15, 2019, 4:01pm UTC](https://discourse.julialang.org/t/ann-sortinglab-jl-fast-sorting-algorithms-for-strings-and-categoricalarrays/19653/2 "2019-01-15T16:01:46Z")

</div>

Great, work! Thank you for amazing library! Have you considered replicating the Base’s sorting API, so that users can just swap out the backend without having to refactor the code. This would certainly make your library more popular 🙂

---

<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: [January 15, 2019, 10:12pm UTC](https://discourse.julialang.org/t/ann-sortinglab-jl-fast-sorting-algorithms-for-strings-and-categoricalarrays/19653/3 "2019-01-15T22:12:43Z")

</div>

> [@Azamat](#):
>
> can just swap out the backend without having to refactor the code

There is [a thread about improving the sort API](https://discourse.julialang.org/t/taking-sorting-ordering-seriously/14975).

I tried to [incorporate my sorting algorithm into SortingAlgorithms.jl](https://github.com/JuliaCollections/SortingAlgorithms.jl/pull/27) but I found that there is a fundamental issue with [how base dispatches on the sorting data](https://github.com/JuliaCollections/SortingAlgorithms.jl/pull/27#issuecomment-357401608). What I wrote is difficult to understand and that is because the way the dispatch works is quite complicated and can’t be summarises and understood easily IMO.

I hope there is progress soon on the API and then we can incorporate.
