# The \`sort\` only change order of the first column in matrix?

**URL:** <https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 25, 2023, 5:56am UTC](https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364 "2023-10-25T05:56:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiz/32/211800_2.png) [@tiZ](https://discourse.julialang.org/u/tiZ)\
**Post date:** [October 25, 2023, 5:56am UTC](https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364/1 "2023-10-25T05:56:19Z")

</div>

Hi friends,

I just found a strange behave of the method of `sort`

```julia
a1 = [ 
    669.57 52.3
    657.52 56.18
    638.38 65.09
    634.17 72.41
    632.08 80.61]
sort(a1,dims=1)

```

will give me below result

```julia
5×2 Matrix{Float64}:
 632.08 52.3
 634.17 56.18
 638.38 65.09
 657.52 72.41
 669.57 80.61

```

why the order of 2nd column donot change?

my Julia version is Version 1.9.3 (2023-08-24)

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [October 25, 2023, 5:59am UTC](https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364/2 "2023-10-25T05:59:28Z")

</div>

Your second column is already sorted. What change do you expect?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 25, 2023, 6:03am UTC](https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364/3 "2023-10-25T06:03:13Z")

</div>

I think you’re looking for `sortslices`

---

<div class="post-metadata">

**Author:** ![tiZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiz/32/211800_2.png) [@tiZ](https://discourse.julialang.org/u/tiZ)\
**Post date:** [October 25, 2023, 6:03am UTC](https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364/4 "2023-10-25T06:03:35Z")

</div>

Oh, I see. I just misunderstand the `sort` is sorting each row.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [October 25, 2023, 6:05am UTC](https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364/5 "2023-10-25T06:05:42Z")

</div>

You explictly told Julia to sort columns with `dims=1`. Julia is column-major so the first dimension goes down the column. If you want to sort within the rows, use `dims=2`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 25, 2023, 11:02am UTC](https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364/6 "2023-10-25T11:02:00Z")

</div>

> [@abraemer](#):
>
> Julia is column-major so the first dimension goes down the column

That is generally the case also for row-major convention. Column/row-majority refers to memory layout, not indexing order.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 25, 2023, 11:03am UTC](https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364/7 "2023-10-25T11:03:45Z")

</div>

> [@gustaphe](#):
>
> I think you’re looking for `sortslices`

What’s wrong with using `sort` with `dims` keyword?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 25, 2023, 2:10pm UTC](https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364/8 "2023-10-25T14:10:30Z")

</div>

I think OP implicitly assumed a structure to his matrix where elements are grouped row-wise so sorting any column would mean all columns are sorted by the sorting order of that column (like when sorting a `DataFrame` by the values of a single column). E.g.

```julia
julia> x = [2 "b"; 1 "c"; 3 "a"]
3×2 Matrix{Any}:
 2 "b"
 1 "c"
 3 "a"

```

gives

```julia
julia> sort(x, dims = 1)
3×2 Matrix{Any}:
 1 "a"
 2 "b"
 3 "c"

```

with sort but

```julia
julia> sortslices(x, dims = 1)
3×2 Matrix{Any}:
 1 "c"
 2 "b"
 3 "a"

```

with sortslices (note the pairwise correspondence `1 => c, 2 => b, 3 => a` is retained here).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 25, 2023, 2:58pm UTC](https://discourse.julialang.org/t/the-sort-only-change-order-of-the-first-column-in-matrix/105364/9 "2023-10-25T14:58:52Z")

</div>

Ah, perhaps. I say, that’s well reasoned, I would never have guessed.
