# \#sort

**URL:** https://discourse.julialang.org/tag/sort/149.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Contradictory nested function performance and allocation](https://discourse.julialang.org/t/contradictory-nested-function-performance-and-allocation/136548)

<div class="topic-metadata">

**Author:** [@lilachint](https://discourse.julialang.org/u/lilachint)\
**Replies:** 2\
**Last updated:** [April 4, 2026, 8:38am UTC](https://discourse.julialang.org/t/contradictory-nested-function-performance-and-allocation/136548 "2026-04-04T08:38:44Z")

</div>

Recently when working on sorting data with Hilbert curves, I noticed something very peculiar when benchmarking. function hilbert\_sort(m::AbstractMatrix{\<:Integer}, b::Integer; rev::Bool = false) return m\[:, hilbert\_…

---

## [Find n smallest values in an n dims array](https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092)

<div class="topic-metadata">

**Author:** [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Replies:** 22\
**Last updated:** [February 13, 2026, 2:07pm UTC](https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092 "2026-02-13T14:07:33Z")

</div>

Hi Every one! I have the bad tendency of working with N dims arrays. Right now I have a 10x20x6 arrays and I need to find the index of the 5 smallest values. I have tried PartialQuickSort and argmin but I can’t get the…

---

## [Topological sort (performance)](https://discourse.julialang.org/t/topological-sort-performance/17153)

<div class="topic-metadata">

**Author:** [@govorunov](https://discourse.julialang.org/u/govorunov)\
**Replies:** 43\
**Last updated:** [November 10, 2025, 5:36pm UTC](https://discourse.julialang.org/t/topological-sort-performance/17153 "2025-11-10T17:36:27Z")

</div>

Hi. I needed to implement a topological sort, so I found some implementations on the Internet here: Topological sort - Rosetta Code I initially needed it in Python and experimented with Julia just out of curiosity. Her…

---

## [Package for partial sorting output iterator](https://discourse.julialang.org/t/package-for-partial-sorting-output-iterator/130408)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 7\
**Last updated:** [July 10, 2025, 1:14am UTC](https://discourse.julialang.org/t/package-for-partial-sorting-output-iterator/130408 "2025-07-10T01:14:48Z")

</div>

Is there an implementation that partial sorts an iterator without allocating the whole output? Eg suppose that I need the n largest elements by f from an iterator itr that has N \\gg n elements. partialsort!(collect(itr…

---

## [Sorting when the comparison operator depends on the entire input vector](https://discourse.julialang.org/t/sorting-when-the-comparison-operator-depends-on-the-entire-input-vector/130152)

<div class="topic-metadata">

**Author:** [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Replies:** 6\
**Last updated:** [June 24, 2025, 6:04am UTC](https://discourse.julialang.org/t/sorting-when-the-comparison-operator-depends-on-the-entire-input-vector/130152 "2025-06-24T06:04:22Z")

</div>

Hi all. Given a parameter Vector{Float64} called x, I want to find x\* such that f(x\*) \> f(x) for all possible permutations of x, where f maps x to a single Float64. I could (and have successfully) used a general optimiza…

---

## [Interesting talk for the Julia sorting community](https://discourse.julialang.org/t/interesting-talk-for-the-julia-sorting-community/129385)

<div class="topic-metadata">

**Author:** [@Dan](https://discourse.julialang.org/u/Dan)\
**Replies:** 3\
**Last updated:** [May 27, 2025, 8:01pm UTC](https://discourse.julialang.org/t/interesting-talk-for-the-julia-sorting-community/129385 "2025-05-27T20:01:07Z")

</div>

Here is an interesting talk given lately: Giving details about TimSort and the new PowerSort and other interesting details. This may be for @Lilith and other people who like things to be in proper order. P…

---

## [Sorting in-place several Vector's simultaneously](https://discourse.julialang.org/t/sorting-in-place-several-vectors-simultaneously/125665)

<div class="topic-metadata">

**Author:** [@Leo\_I](https://discourse.julialang.org/u/Leo_I)\
**Replies:** 21\
**Last updated:** [March 3, 2025, 1:26pm UTC](https://discourse.julialang.org/t/sorting-in-place-several-vectors-simultaneously/125665 "2025-03-03T13:26:53Z")

</div>

I have several vectors (with different types of elements and lengths) whose entries I wish to sort in-place according to some function, like first or identity (lexicographical sort). For example: n=10^7; x=rand(1:n,n);…

---

## [How to sort NTuple](https://discourse.julialang.org/t/how-to-sort-ntuple/11535)

<div class="topic-metadata">

**Author:** [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Replies:** 5\
**Last updated:** [January 5, 2025, 12:57pm UTC](https://discourse.julialang.org/t/how-to-sort-ntuple/11535 "2025-01-05T12:57:53Z")

</div>

I have several integers input from a function like this function foo(a::Int...) end How do I sort a? I have to collect it first and sort it as a vector at the moment. Will it possible to sort it directly?

---

## [How is it possible that parallelized code causes fewer allocations?](https://discourse.julialang.org/t/how-is-it-possible-that-parallelized-code-causes-fewer-allocations/124161)

<div class="topic-metadata">

**Author:** [@Leo\_I](https://discourse.julialang.org/u/Leo_I)\
**Replies:** 19\
**Last updated:** [December 30, 2024, 5:41pm UTC](https://discourse.julialang.org/t/how-is-it-possible-that-parallelized-code-causes-fewer-allocations/124161 "2024-12-30T17:41:58Z")

</div>

This case baffles me: using StructArrays n, s = 10^5, 100; llp = \[rand(1:n,s) for \_=1:n\]; llw = \[rand(-1:0.001:1,s) for \_=1:n\] function sort\_vec!(pp::Vector{tp}, ww::Vector{tw}, by::Function=first) ::Nothing where {tp\<…

---

## [Sort tuple](https://discourse.julialang.org/t/sort-tuple/110730)

<div class="topic-metadata">

**Author:** [@jar1](https://discourse.julialang.org/u/jar1)\
**Replies:** 11\
**Last updated:** [December 8, 2024, 9:56pm UTC](https://discourse.julialang.org/t/sort-tuple/110730 "2024-12-08T21:56:14Z")

</div>

Any reason this shouldn’t work? julia\> sort((1,2,3)) ERROR: MethodError: no method matching sort(::Tuple{Int64, Int64, Int64})

---

## [Iterate over a range in a non standard way](https://discourse.julialang.org/t/iterate-over-a-range-in-a-non-standard-way/121137)

<div class="topic-metadata">

**Author:** [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Replies:** 4\
**Last updated:** [October 10, 2024, 12:45pm UTC](https://discourse.julialang.org/t/iterate-over-a-range-in-a-non-standard-way/121137 "2024-10-10T12:45:01Z")

</div>

I’m doing some exhaustive search loops and would like to cover say a range 10:100 but suspect (with quite some confidence) that the solution I look for is around 90. So I would like to loop over the range by starting at …

---

## [Counting Exchanges To Order Array with Repeats](https://discourse.julialang.org/t/counting-exchanges-to-order-array-with-repeats/118342)

<div class="topic-metadata">

**Author:** [@brombo](https://discourse.julialang.org/u/brombo)\
**Replies:** 9\
**Last updated:** [August 19, 2024, 1:57pm UTC](https://discourse.julialang.org/t/counting-exchanges-to-order-array-with-repeats/118342 "2024-08-19T13:57:56Z")

</div>

I am asking this question to avoid reinventing the wheel (also because I am lazy). I have to arrays of integers in normal order with no repeats within each array. Then I generate an array by concatenation the two array…

---

## [How do I sort a dictionary by its keys?](https://discourse.julialang.org/t/how-do-i-sort-a-dictionary-by-its-keys/37848)

<div class="topic-metadata">

**Author:** [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Replies:** 10\
**Last updated:** [July 28, 2024, 8:59am UTC](https://discourse.julialang.org/t/how-do-i-sort-a-dictionary-by-its-keys/37848 "2024-07-28T08:59:15Z")

</div>

I want to sort this by its keys: 5-element Dictionaries.HashDictionary{Int64,Any} 4 │ 0.2199244 2 │ 0.2000908 3 │ 0.19014 5 │ 0.2099752 1 │ 0.1798696

---

## [Wanted: an API for merging sorted collections](https://discourse.julialang.org/t/wanted-an-api-for-merging-sorted-collections/115722)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 1\
**Last updated:** [June 16, 2024, 6:57pm UTC](https://discourse.julialang.org/t/wanted-an-api-for-merging-sorted-collections/115722 "2024-06-16T18:57:56Z")

</div>

I don’t think there’s a public function for the merge algorithm anywhere in Julia or the ecosystem. It’s what merge sort is based on. Wikipedia introduces merging like this: Merge algorithms are a family of algorithms …

---

## [Sorting a StructArray variable faster and how to update values?](https://discourse.julialang.org/t/sorting-a-structarray-variable-faster-and-how-to-update-values/112621)

<div class="topic-metadata">

**Author:** [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Replies:** 6\
**Last updated:** [April 7, 2024, 1:06pm UTC](https://discourse.julialang.org/t/sorting-a-structarray-variable-faster-and-how-to-update-values/112621 "2024-04-07T13:06:16Z")

</div>

Hello! I have a runnable MWE here, put into a small module to easily redefine struct as needed: module ParticleSorting export sort\_particles\_by\_density using StructArrays using StaticArrays using BenchmarkTools using…

---

## [Why aren't the permutations from sortslices() available?](https://discourse.julialang.org/t/why-arent-the-permutations-from-sortslices-available/99029)

<div class="topic-metadata">

**Author:** [@g2g](https://discourse.julialang.org/u/g2g)\
**Replies:** 1\
**Last updated:** [March 16, 2024, 6:30pm UTC](https://discourse.julialang.org/t/why-arent-the-permutations-from-sortslices-available/99029 "2024-03-16T18:30:05Z")

</div>

I’m writing some data comparison code and it would be helpful if Julia had a sortslicesperm() similar to sortperm() so that a matrix could be sorted (like a vector) by M = rand(0:2, 16, 4); p = sortslicesperm(M, dims=1)…

---

## [\[ANN\] OptimalSortingNetworks: Sort small collections efficiently and with good type inference](https://discourse.julialang.org/t/ann-optimalsortingnetworks-sort-small-collections-efficiently-and-with-good-type-inference/105544)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 14\
**Last updated:** [February 25, 2024, 6:59pm UTC](https://discourse.julialang.org/t/ann-optimalsortingnetworks-sort-small-collections-efficiently-and-with-good-type-inference/105544 "2024-02-25T18:59:05Z")

</div>

OptimalSortingNetworks (OSN for short) is being registered. Use it to sort small tuples or vectors: julia\> using OptimalSortingNetworks julia\> sorted(\[10, 5, 7, 9\]) 4-element Vector{Int64}: 5 7 9 10 julia\> sort…

---

## [Sorted Array implementation?](https://discourse.julialang.org/t/sorted-array-implementation/5897)

<div class="topic-metadata">

**Author:** [@raff](https://discourse.julialang.org/u/raff)\
**Replies:** 21\
**Last updated:** [February 6, 2024, 3:57pm UTC](https://discourse.julialang.org/t/sorted-array-implementation/5897 "2024-02-06T15:57:41Z")

</div>

Is there any implementation of sorted array? I need to store a sorted list of integer. I will frequently check what is the k-th value in the list and in what position a particular value is in the list. I checked Sorted…

---

## [Custom data structure for repeatedly partial sorting constant-size array](https://discourse.julialang.org/t/custom-data-structure-for-repeatedly-partial-sorting-constant-size-array/107594)

<div class="topic-metadata">

**Author:** [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Replies:** 1\
**Last updated:** [December 14, 2023, 2:10am UTC](https://discourse.julialang.org/t/custom-data-structure-for-repeatedly-partial-sorting-constant-size-array/107594 "2023-12-14T02:10:35Z")

</div>

I have an application where, as a subroutine, I need to (partial) sort a specific n-dimensional vector repeatedly (for n\>10^7). Given initial data x^{(0)} and some perturbation \\epsilon^{(i)} in step i, I want to obtain x…

---

## [\[ANN\] TupleSorting: Sort tuples efficiently and with good type inference](https://discourse.julialang.org/t/ann-tuplesorting-sort-tuples-efficiently-and-with-good-type-inference/105756)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 3\
**Last updated:** [November 15, 2023, 9:51am UTC](https://discourse.julialang.org/t/ann-tuplesorting-sort-tuples-efficiently-and-with-good-type-inference/105756 "2023-11-15T09:51:03Z")

</div>

TupleSorting is being registered. Use it to sort tuples (even large ones) efficiently and with good type inference: julia\> using TupleSorting julia\> const TS = TupleSorting TupleSorting julia\> t = ntuple((\_ -\> rand(0:…

---

## [Sort matrix based on the elements of a specific column](https://discourse.julialang.org/t/sort-matrix-based-on-the-elements-of-a-specific-column/23475)

<div class="topic-metadata">

**Author:** [@eed](https://discourse.julialang.org/u/eed)\
**Replies:** 8\
**Last updated:** [October 30, 2023, 2:12am UTC](https://discourse.julialang.org/t/sort-matrix-based-on-the-elements-of-a-specific-column/23475 "2023-10-30T02:12:35Z")

</div>

Hi I’m new to Julia but I have some exprience with Matlab. I have an array A (mix of numbers and text columns) and I want to sort the rows based on the values on a specific column of A. In matlab, the code is: B = so…

---

## [Do we have differentiable sort in Julia?](https://discourse.julialang.org/t/do-we-have-differentiable-sort-in-julia/102713)

<div class="topic-metadata">

**Author:** [@pat-alt](https://discourse.julialang.org/u/pat-alt)\
**Replies:** 14\
**Last updated:** [September 12, 2023, 11:47am UTC](https://discourse.julialang.org/t/do-we-have-differentiable-sort-in-julia/102713 "2023-09-12T11:47:04Z")

</div>

Python has support for fast differentiable sorting which is an implementation of Blondel et al. (2020). I don’t think we have this in Julia at the moment? I have specific use case for ConformalPrediction.jl, but beyond …

---

## [Sort elements by frequency in julia](https://discourse.julialang.org/t/sort-elements-by-frequency-in-julia/103433)

<div class="topic-metadata">

**Author:** [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Replies:** 4\
**Last updated:** [September 5, 2023, 9:21pm UTC](https://discourse.julialang.org/t/sort-elements-by-frequency-in-julia/103433 "2023-09-05T21:21:20Z")

</div>

I want to sort elements by frequency . For example let x = \["a", "d", "d", "c", "c", "c", "f", "b", "b" ,"b", "b"\] I want to have output sorted as x = \["b", "b" ,"b", "b","c", "c", "c, "d", "d", "a", "f"\] where we ha…

---

## [What's wrong with this ordering function?](https://discourse.julialang.org/t/whats-wrong-with-this-ordering-function/103160)

<div class="topic-metadata">

**Author:** [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Replies:** 5\
**Last updated:** [August 24, 2023, 3:21pm UTC](https://discourse.julialang.org/t/whats-wrong-with-this-ordering-function/103160 "2023-08-24T15:21:57Z")

</div>

Hi, I have a vector of tuples like this: julia\> results 75-element Vector{NamedTuple{(:program, :mean, :stddev, :score), Tuple{String, Float64, Float64, Float64}}}: (program = "fac\_ite\_#0", mean = 0.1477079562429519, s…

---

## [DataFrame sort Performance using Query.jl vs SAS PROC SORT](https://discourse.julialang.org/t/dataframe-sort-performance-using-query-jl-vs-sas-proc-sort/41225)

<div class="topic-metadata">

**Author:** [@Dominic\_Pazzula](https://discourse.julialang.org/u/Dominic_Pazzula)\
**Replies:** 19\
**Last updated:** [August 11, 2023, 9:14pm UTC](https://discourse.julialang.org/t/dataframe-sort-performance-using-query-jl-vs-sas-proc-sort/41225 "2023-08-11T21:14:38Z")

</div>

I’m still learning Julia and am converting some SAS code. I have a large DataFrame (3.3M rows, 6 columns) that I am trying to sort. The performance compared to SAS is abysmal. function sort1(simstates) simstates2…

---

## [What should \`issorted\` return when an order isn't defined for the elements, but a comparison works?](https://discourse.julialang.org/t/what-should-issorted-return-when-an-order-isnt-defined-for-the-elements-but-a-comparison-works/101423)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 4\
**Last updated:** [July 10, 2023, 3:02pm UTC](https://discourse.julialang.org/t/what-should-issorted-return-when-an-order-isnt-defined-for-the-elements-but-a-comparison-works/101423 "2023-07-10T15:02:09Z")

</div>

Currently, issorted returns true by default, even if the elements do not have a defined order. For example, julia\> issorted(\[NaN, NaN\]) true In this case, because NaN is a floating-point number, the comparison works, e…

---

## [What is Julia's maxk(Matlab) that returns the indice of top K largest values?](https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100)

<div class="topic-metadata">

**Author:** [@complexfilter](https://discourse.julialang.org/u/complexfilter)\
**Replies:** 20\
**Last updated:** [May 9, 2023, 4:19pm UTC](https://discourse.julialang.org/t/what-is-julias-maxk-matlab-that-returns-the-indice-of-top-k-largest-values/14100 "2023-05-09T16:19:51Z")

</div>

I am currently converting my Matlab code into Julia code. In an effort to converting the Matlab off-the-shelf function maxk, which returns the values and indices of the top K largest/smallest values, I found it difficult…

---

## [Parallel quicksort openmp](https://discourse.julialang.org/t/parallel-quicksort-openmp/88499)

<div class="topic-metadata">

**Author:** [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Replies:** 30\
**Last updated:** [March 24, 2023, 8:26pm UTC](https://discourse.julialang.org/t/parallel-quicksort-openmp/88499 "2023-03-24T20:26:47Z")

</div>

Hi, I was wondering how it is possible to parallelise a quicksort algorithm below (from rosetta code) in Julia? In c with openmp one would use #pragma omp task. Example : But what is the equivalent in Julia? Thank …

---

## [Sort array based on another array?](https://discourse.julialang.org/t/sort-array-based-on-another-array/26854)

<div class="topic-metadata">

**Author:** [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Replies:** 14\
**Last updated:** [March 16, 2023, 2:42am UTC](https://discourse.julialang.org/t/sort-array-based-on-another-array/26854 "2023-03-16T02:42:43Z")

</div>

Hello I am post-processing some particle simulation data and for an example I would get the following two arrays: Rhop\_vec\[50\] 125749-element Array{Float32,1}: 1007.7992 1014.8219 1011.84064 1019.48114 1011.2265 …

---

## [Sort keys of NamedTuple](https://discourse.julialang.org/t/sort-keys-of-namedtuple/94630)

<div class="topic-metadata">

**Author:** [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Replies:** 5\
**Last updated:** [February 15, 2023, 5:41pm UTC](https://discourse.julialang.org/t/sort-keys-of-namedtuple/94630 "2023-02-15T17:41:20Z")

</div>

I would like to sort a NamedTuple alphabetically by its keys. For instance: (b=1, a=2, c=3) ---\> (a=2, b=1, c=3) (a=1, c=2, b=3) ---\> (a=1, b=3, c=2) I am pretty sure such functionality already exists, where to look fo…

[Next page](https://discourse.julialang.org/tag/sort/149.md?match_all_tags=true&page=1&tags%5B%5D=sort)
