# Complex number sort

**URL:** https://discourse.julialang.org/t/complex-number-sort/61417
**Category:** General Usage
**Tags:** question, sort, sortperm, complex-numbers
**Created:** [May 19, 2021, 6:20am UTC](https://discourse.julialang.org/t/complex-number-sort/61417 "2021-05-19T06:20:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![cepheid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cepheid/32/24934_2.png) [@cepheid](https://discourse.julialang.org/u/cepheid)
#### Post date: [May 19, 2021, 6:20am UTC](https://discourse.julialang.org/t/complex-number-sort/61417/1 "2021-05-19T06:20:45Z")

</div>

I want to sort the complex vector as the lexicographically order, that is first the real part from large to small, the same real part, image part from small to large.

```julia
julia> a = ComplexF64[-0.05050274530206651 + 0.6013860908906193im, -0.050502745302066526 - 0.6013860908906193im, -1.1280686832693614 + 0.7017876343969567im, -1.1280686832693616 - 0.7017876343969567im]
4-element Vector{ComplexF64}:
  -0.05050274530206651 + 0.6013860908906193im
 -0.050502745302066526 - 0.6013860908906193im
   -1.1280686832693614 + 0.7017876343969567im
   -1.1280686832693616 - 0.7017876343969567im

julia> sort(a, by = x -> (real(x), -imag(x)), rev=true)
4-element Vector{ComplexF64}:
   -0.05050274530206651 + 0.6013860908906193im
   -0.050502745302066526 - 0.6013860908906193im
   -1.1280686832693614 + 0.7017876343969567im
   -1.1280686832693616 - 0.7017876343969567im

```

The -0.05050274530206651 and -0.050502745302066526 may consider the same value.  
How to achieve the order

```julia
   -0.050502745302066526 - 0.6013860908906193im
   -0.05050274530206651 + 0.6013860908906193im
   -1.1280686832693616 - 0.7017876343969567im
   -1.1280686832693614 + 0.7017876343969567im

```

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [May 19, 2021, 6:43am UTC](https://discourse.julialang.org/t/complex-number-sort/61417/3 "2021-05-19T06:43:02Z")

</div>

> The -0.05050274530206651 and -0.050502745302066526 may consider the same value.

This is not quite clear, but probably you mean, that you want to compare values with less precision?

```julia
julia> sort(a, by = x -> (floor(real(x), digits = 6), -floor(imag(x), digits = 6)), rev=true)
4-element Vector{ComplexF64}:
 -0.050502745302066526 - 0.6013860908906193im
  -0.05050274530206651 + 0.6013860908906193im
   -1.1280686832693616 - 0.7017876343969567im
   -1.1280686832693614 + 0.7017876343969567im

```

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [May 19, 2021, 7:59am UTC](https://discourse.julialang.org/t/complex-number-sort/61417/4 "2021-05-19T07:59:28Z")

</div>

Some more information regarding your application would be useful. How do you obtain your vector to be sorted (this determines what kind of rounding errors we should expect), and for what purpose do you want to sort this vector according to the specified order (this might indicate suitable tolerances).

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [May 19, 2021, 1:30pm UTC](https://discourse.julialang.org/t/complex-number-sort/61417/5 "2021-05-19T13:30:22Z")

</div>

For the sake of discussion and information, I asked a quite close question on stack overflow a few months ago about finding the maximum / minimum value inside a complex array.

As far as I understood there, this will be base provided in Julia 1.7

[julia - Generic maximum/minimum function for complex numbers - Stack Overflow](https://stackoverflow.com/questions/66260858/generic-maximum-minimum-function-for-complex-numbers)
