diff --git a/src/MetidaBase.jl b/src/MetidaBase.jl index d0710e4..76ef6da 100644 --- a/src/MetidaBase.jl +++ b/src/MetidaBase.jl @@ -6,7 +6,7 @@ module MetidaBase using StatsModels, Tables import Tables: istable, columnaccess, columns, getcolumn, columnnames - import Base: getindex, length + import Base: getindex, length, ht_keyindex include("abstracttype.jl") include("types.jl") diff --git a/src/types.jl b/src/types.jl index 7b6b147..99e9080 100644 --- a/src/types.jl +++ b/src/types.jl @@ -17,8 +17,7 @@ Tables.getcolumn(t, nm::Symbol) = t.table[nm] Tables.columnnames(t::MetidaTable) = collect(keys(t.table)) - -# MetidaFreq.jl +# All struct DataSet{T <: AbstractData} data::Vector{T} @@ -32,6 +31,32 @@ function Base.length(ds::DataSet) length(ds.data) end +function getindormiss(d, i) + ind = ht_keyindex(d, i) + if ind > 0 return d.vals[ind] else return missing end +end +function islessdict(a::Dict{A1,A2}, b::Dict{B1,B2}, k::Union{AbstractVector, Set}) where A1 where A2 where B1 where B2 + l = length(k) + av = Vector{Union{Missing, A2}}(undef, l) + bv = Vector{Union{Missing, B2}}(undef, l) + @inbounds for i = 1:l + av[i] = getindormiss(a, k[i]) + bv[i] = getindormiss(b, k[i]) + end + isless(av, bv) +end +function islessdict(a::Dict, b::Dict, k) + isless(getindormiss(a, k), getindormiss(b, k)) +end +function Base.sort!(a::DataSet{T}, k; alg::Base.Algorithm = QuickSort, lt=nothing, by=nothing, rev::Bool=false, order::Base.Ordering = Base.Forward) where T <: AbstractIdData + if isnothing(by) by = x -> x.id end + if isnothing(lt) lt = (x, y) -> islessdict(x, y, k) end + sort!(a.data; alg = alg, lt = lt, by = by, rev = rev, order = order) + a +end + +# MetidaFreq.jl + struct Proportion <: AbstractData x::Int n::Int