Elaborating on this SO question, I am trying to use a custom less-than function, to be used with the lt
sort keyword, but I don’t know how to provide to the function a third parameter that is the wanted “custom order” (for multiple column sort):
For example, this works:
using DataFrames
df = DataFrame(
c1 = ['a','b','c','a','b','c'],
c2 = ["aa","aa","bb","bb","cc","cc"],
c3 = [1,2,3,10,20,30]
)
6×3 DataFrames.DataFrame
│ Row │ c1 │ c2 │ c3 │
├─────┼─────┼──────┼────┤
│ 1 │ 'a' │ "aa" │ 1 │
│ 2 │ 'b' │ "aa" │ 2 │
│ 3 │ 'c' │ "bb" │ 3 │
│ 4 │ 'a' │ "bb" │ 10 │
│ 5 │ 'b' │ "cc" │ 20 │
│ 6 │ 'c' │ "cc" │ 30 │
ordc2 = ["bb","aa","cc"]
function customLt(r1,r2)
return ( find(x -> x == r1, ordc2)[1] < find(x -> x == r2, ordc2)[1] )
end
sortedDf = sort(df, cols = [order(:c2, lt=customLt)])
6×3 DataFrames.DataFrame
│ Row │ c1 │ c2 │ c3 │
├─────┼─────┼──────┼────┤
│ 1 │ 'c' │ "bb" │ 3 │
│ 2 │ 'a' │ "bb" │ 10 │
│ 3 │ 'a' │ "aa" │ 1 │
│ 4 │ 'b' │ "aa" │ 2 │
│ 5 │ 'b' │ "cc" │ 20 │
│ 6 │ 'c' │ "cc" │ 30 │
But this doesn’t:
using DataFrames
df = DataFrame(
c1 = ['a','b','c','a','b','c'],
c2 = ["aa","aa","bb","bb","cc","cc"],
c3 = [1,2,3,10,20,30]
)
ordc1 = ['b','a','c']
ordc2 = ["bb","aa","cc"]
function customLt(r1,r2,col)
return ( find(x -> x == r1, col)[1] < find(x -> x == r2, col)[1] )
end
sortedDf = sort(df, cols = [order(:c2, lt=customLt(ordc2)),order(:c1, lt=customLt(ordc1))] )
MethodError: no method matching customLt(::Array{String,1})
Closest candidates are:
customLt(::Any, !Matched::Any) at /home/lobianco/git/ffsm_pp/00_private/2016_foretcc/data/output/test.jl:59
customLt(::Any, !Matched::Any, !Matched::Any) at /home/lobianco/git/ffsm_pp/00_private/2016_foretcc/data/output/test.jl:73
[...]