lua多条件组合排序

对 宠物列表 按照星级或者等级或者稀有度等属性进行排序

为了方便适配不同条件的组合排序,对排序的维度进行拆分成不同的方法,然后根据排序类型进行组合

详细需求

排序规则

  • 稀有度排序:稀有度id降序 > 等级降序 > 星级降序 > 职业id升序 > 技能类型id升序

  • 等级排序:等级降序 > 稀有度id降序 > 星级降序 > 职业id升序 > 技能类型id升序

  • 星级排序:星级降序 > 稀有度id降序 > 等级降序 > 职业id升序 > 技能类型id升序

  • 职业排序:职业id升序 > 稀有度id降序 > 等级降序 > 星级降序 > 技能类型id升序

  • 技能类型排序:技能类型id升序 > 稀有度id降序 > 等级降序 > 星级降序 > 职业id升序

如果以上维度都相同的话,按照配置表的宠物Id进行排序

复制代码
---神宠排序类型
---@class PetSortType

PetSortType = {
    AptitudeType = 1, --资质
    LevelType = 2, --等级
    StarType = 3, --星级
    ProfessionType = 4, --职业
    SkillType = 5, --技能
}

---获取神宠属性列表
---@param petModel PetModel
---@param petType PetSortType
---@return table<number, number>
function PetBagProxy:sortPetModelList(petModelList, petSortType)
    if not petModelList or #petModelList <= 0 then
        return
    end
    local sortFunctions = {
        [1] = function(a,b)
            if a.aptitude ~= b.aptitude then
                return true, a.aptitude > b.aptitude
            end
            return false
        end,
        [2] = function(a,b)
            if a.level ~= b.level then
                return true, a.level > b.level
            end
            return false
        end,
        [3] = function(a,b)
            if a.star ~= b.star then
                return true, a.star > b.star
            end
            return false
        end,
        [4] = function(a,b)
            if a.profession ~= b.profession then
                return true, a.profession < b.profession
            end
            return false
        end,
        [5] = function(a,b)
            if a.type ~= b.type then
                return true, a.type < b.type
            end
            return false
        end,
    }
    local sortOrderList = self:getPetSortTypeOrderList(petSortType)
    table.sort(petModelList, function(a, b)
        for i, orderId in ipairs(sortOrderList) do
            local isSort, isSortResult = sortFunctions[orderId](a, b)
            if isSort then
                return isSortResult
            end
        end
        --如果以上条件都相同,按照模板id排序
        return a.templateId < b.templateId
    end)
end

---获取不同排序类型对应的呃方法顺序
---@param petSortType PetSortType
function PetBagProxy:getPetSortTypeOrderList(petSortType)
    if petSortType == PetSortType.AptitudeType then
        return {1,2,3,4,5}
    elseif petSortType == PetSortType.LevelType then
        return {2,1,3,4,5}
    elseif petSortType == PetSortType.StarType then
        return {3,1,2,4,5}
    elseif petSortType == PetSortType.ProfessionType then
        return {4,1,2,3,5}
    elseif petSortType == PetSortType.SkillType then
        return {5,1,2,3,4}
    end
end
相关推荐
颜酱1 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法
地平线开发者17 小时前
SparseDrive 模型导出与性能优化实战
算法·自动驾驶
董董灿是个攻城狮17 小时前
大模型连载2:初步认识 tokenizer 的过程
算法
地平线开发者18 小时前
地平线 VP 接口工程实践(一):hbVPRoiResize 接口功能、使用约束与典型问题总结
算法·自动驾驶
罗西的思考18 小时前
AI Agent框架探秘:拆解 OpenHands(10)--- Runtime
人工智能·算法·机器学习
HXhlx21 小时前
CART决策树基本原理
算法·机器学习
Wect21 小时前
LeetCode 210. 课程表 II 题解:Kahn算法+DFS 双解法精讲
前端·算法·typescript
颜酱1 天前
单调队列:滑动窗口极值问题的最优解(通用模板版)
javascript·后端·算法
Gorway1 天前
解析残差网络 (ResNet)
算法
拖拉斯旋风1 天前
LeetCode 经典算法题解析:优先队列与广度优先搜索的巧妙应用
算法