(Brief History) After writing the code and testing I, the same as others in this thread, realized Matlab gives nonsense SORT ordering for REAL functions.

So I searched Julia discourse again (4th time is a charm), found this thread, and so whipped up the code after Keying off StefanKarpinski comment / NOTE Here:

StefanKarpinski Stefan_Karpinski (Viking helmet man to the Rescue !!)

Steward

Aug 2nd (2024)

It doesn’t even match what you’d get if all the complex values happen to be real since it orders by absolute value.

Truly WHACKY way of ordering complex numbers.

Ordering LEXICOlogically by (re, im) SEEMS much BETTER.

```
function complex_min(x,y)
## treat NaN as being GREATER than any other value and so are sorted to the end of the list.
## So isnan IMMEDIATELY DECIDES which variable to return as LESSER
if isnan(x)
return y
end
if isnan(y)
return x
end
if real(y) < real(x) ## NOTE Strictly less than
return y ## x is NOT LT y
end
if real(y) > real(x) ## NOTE Strictly greater than
return x
end
## Here IFF real(y) = real(x), so sort by imag
if imag(y) < imag(x) ## NOTE Strictly less than
return y ## x is NOT LT y
else
return x ## x IS LT y
end
end ## end function
```

## !! TDD UNIT TEST - YAY PASSED !!

tx=NaN + 0.0im; ty=1.0 + 0.0im

julia> complex_min(ty,tx)

1.0 + 0.0im

## Reverse order SB same answer.

julia> complex_min(tx,ty)

1.0 + 0.0im

```
function complex_isless(x,y)
## treat NaN as being GREATER than any other value and so are sorted to the end of the list.
## So isnan IMMEDIATELY DECIDES which variable to return as LESSER
if isnan(x)
return false
end
if isnan(y)
return true
end
if real(y) < real(x) ## NOTE Strictly less than
return false ## x is NOT LT y
end
if real(y) > real(x) ## NOTE Strictly greater than
return true
end
## Here IFF real(y) = real(x), so sort by imag
if imag(y) < imag(x) ## NOTE Strictly less than
return false ## x is NOT LT y
else
return true ## x IS LT y
end
end ## end function
```

## Marc Cox TDD QA TEST My Example code above Passes some TDD QA TESTS for Sort order of a 1Dim Vector of Complex numbers.

vec_1 = [-3, NaN, NaN,1+im, -3, 1, 1-im, -3-im, -3+im]

sort(vec_1, lt=complex_isless)

julia> vec_1 = [-3, NaN, NaN,1+im, -3, 1, 1-im, -3-im, -3+im]

9-element Vector{ComplexF64}:

-3.0 + 0.0im

NaN + 0.0im

NaN + 0.0im

1.0 + 1.0im

-3.0 + 0.0im

1.0 + 0.0im

1.0 - 1.0im

-3.0 - 1.0im

-3.0 + 1.0im

julia> sort(vec_1, lt=complex_isless)

9-element Vector{ComplexF64}:

-3.0 - 1.0im

-3.0 + 0.0im

-3.0 + 0.0im

-3.0 + 1.0im

1.0 - 1.0im

1.0 + 0.0im

1.0 + 1.0im

NaN + 0.0im

NaN + 0.0im

# RE: Stefan Karpinski TDD Test: for a working example test case TRY vec_2 WARNING try WITHOUT NaN

vec_2 = [-3, 0.0, 0.5,1+im, -3, 1, 1-im, -3-im, -3+im]

julia> real(MEDIAN_2(vec_2)) == MEDIAN_2(real.(vec_2))

true

## Marc Cox WARNING NOTE there are cases, e.g. NaN in the list,where it is critical that median always uses a SORTED list, NOT a middle FUNCTION

julia> real(MEDIAN_2(sort(vec_2, lt=complex_isless))) == MEDIAN_2(real.(sort(vec_2, lt=complex_isless)))

true

More TDD TESTS to do, to reverify intuition and correct operation of SORT ORDER in ALL CASES suggest try Visualizing Complex function SORT ordering with Julia DomainColoring.jl: Smooth Complex Plotting

at Home · DomainColoring.jl