# Sorting a matrix according to a matrix of column-wise sorted indices

**URL:** <https://discourse.julialang.org/t/sorting-a-matrix-according-to-a-matrix-of-column-wise-sorted-indices/103944>\
**Category:** General Usage\
**Created:** [September 17, 2023, 9:53am UTC](https://discourse.julialang.org/t/sorting-a-matrix-according-to-a-matrix-of-column-wise-sorted-indices/103944 "2023-09-17T09:53:49Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [September 17, 2023, 7:34pm UTC](https://discourse.julialang.org/t/sorting-a-matrix-according-to-a-matrix-of-column-wise-sorted-indices/103944/2 "2023-09-17T19:34:55Z")

</div>

> [@kingoslo](#):
>
> I have a matrix that looks like this:
> 
> `A = rand(40000000000000,3000000000000000000000000)`

Your matrix is obviously not that big; it would be more helpful to give actual runnable examples. Also, you should quote your code. [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

> [@kingoslo](#):
>
> Now I want to use these indices in the fastest possible way to get the matrix with the columns sorted in that order. How can I do that?

Probably faster just to sort the columns directly, in-place, instead of computing the permutation indices and then applying them. e.g.

```julia
for col in eachcol(A)
    sort!(col)
end

```

or `foreach(sort!, eachcol(A))`.

---

_[View the full topic](https://discourse.julialang.org/t/sorting-a-matrix-according-to-a-matrix-of-column-wise-sorted-indices/103944)._
