# L-10 MCS 507 Wed 13 Sep 2023 : callqsort.jl """ We will wrap the C function qsort. void qsort(void *base, size_t nmemb, size_t size, int (*compare)(const void*, const void*)); The C qsort needs a callback function compare, to define the order between the elements to be sorted. """ """ mycompare(a, b) Comparison function (Julia) to pass to qsort (C). """ function mycompare(a, b)::Cint return (a < b) ? -1 : ((a > b) ? +1 : 0) end # To pass this Julia function to C, # we use the macro @function. mycompare_c = @cfunction(mycompare, Cint, (Ref{Cdouble}, Ref{Cdouble})); println("A random sequence of numbers :") A = rand(16, 1) println(A) println("Calling qsort ...") ccall(:qsort, Cvoid, (Ptr{Cdouble}, Csize_t, Csize_t, Ptr{Cvoid}), A, length(A), sizeof(eltype(A)), mycompare_c) println("The sorted sequence :") println(A)