# Faster Alternatives to sortperm

**URL:** <https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170>\
**Category:** Performance\
**Tags:** sortperm, sorting\
**Created:** [April 27, 2022, 8:43pm UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170 "2022-04-27T20:43:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josa9321](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josa9321/32/12170_2.png) [@Josa9321](https://discourse.julialang.org/u/Josa9321)\
**Post date:** [April 27, 2022, 8:43pm UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170/1 "2022-04-27T20:43:53Z")

</div>

Hi,

I am manipulating an matrix (one or more lines) that has the priority numbers of another one, for example:

```julia
priority = [0.1 0.9 0.4 0.7 0.3] # matrix filled using rand()
A = [col1 col2 col3 col4 col5] # matrix filled with rand(0:1)

```

So the order of the matrix A that I want is:

```julia
[col1, col5, col3, col4, col2]

```

Or at least I want the order of the columns with their value equal to 1.

One way to proceed with this is to use the line below

```julia
order = sortperm(view(priority, h, :))

```

But I need this code to run as fast as possible. Is there any way to do it?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 27, 2022, 8:53pm UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170/2 "2022-04-27T20:53:10Z")

</div>

> [@Josa9321](#):
>
> But I need this code to run as fast as possible. Is there any way to do it?

If you find yourself repeatedly sorting arrays (which presumably is why it is performance critical), there is a good chance you should use a different data structure.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 28, 2022, 4:54am UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170/3 "2022-04-28T04:54:10Z")

</div>

> [@Josa9321](#):
>
> `sortperm(view(priority, h, :))`

It looks like you’re sorting a row view, this will likely be less efficient than sorting a column view due to the memory layout.

---

<div class="post-metadata">

**Author:** ![Josa9321](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josa9321/32/12170_2.png) [@Josa9321](https://discourse.julialang.org/u/Josa9321)\
**Post date:** [May 2, 2022, 5:52pm UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170/4 "2022-05-02T17:52:05Z")

</div>

@stevengj Which kinds do you suggest?

@baggepinnen Thanks, I’ll change that.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 2, 2022, 6:04pm UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170/5 "2022-05-02T18:04:39Z")

</div>

> [@Josa9321](#):
>
> @stevengj Which kinds do you suggest?

Depends on what you are doing. What kinds of operations do you need on the data? Why are you sorting repeatedly?

---

<div class="post-metadata">

**Author:** ![Josa9321](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josa9321/32/12170_2.png) [@Josa9321](https://discourse.julialang.org/u/Josa9321)\
**Post date:** [May 2, 2022, 7:07pm UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170/6 "2022-05-02T19:07:36Z")

</div>

Because the vector `priority` changes its value at great frequency, so every time I run the function, I need to see its value.

I’m creating a heuristic procedure to generate solutions to a floor shop problem, and this procedure needs to be very randomized to generate good solutions, and the representation of the solution I got from the literature, so I think it needed to be the way I’m doing, sorting in a certain frequence.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 2, 2022, 7:28pm UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170/7 "2022-05-02T19:28:36Z")

</div>

what about a heap based priority queue?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 2, 2022, 8:09pm UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170/8 "2022-05-02T20:09:22Z")

</div>

Are you using `rand` + `sortperm!` instead of `shuffle!` then?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 2, 2022, 8:11pm UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170/9 "2022-05-02T20:11:23Z")

</div>

Good question. If the priorities are random anyway, you’re much better off using `shuffle!`. It will be faster and very slightly more statistically correct.

---

<div class="post-metadata">

**Author:** ![Josa9321](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josa9321/32/12170_2.png) [@Josa9321](https://discourse.julialang.org/u/Josa9321)\
**Post date:** [May 10, 2022, 4:33pm UTC](https://discourse.julialang.org/t/faster-alternatives-to-sortperm/80170/10 "2022-05-10T16:33:18Z")

</div>

Thanks you all. I used another representation for the solution to avoid sortperm.
