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
相关推荐
hn小菜鸡5 小时前
LeetCode 377.组合总和IV
数据结构·算法·leetcode
Deepoch6 小时前
Deepoc 大模型:无人机行业的智能变革引擎
人工智能·科技·算法·ai·动态规划·无人机
heimeiyingwang9 天前
【深度学习加速探秘】Winograd 卷积算法:让计算效率 “飞” 起来
人工智能·深度学习·算法
WIN赢9 天前
PostMan使用
测试工具·lua·postman
时空自由民.9 天前
C++ 不同线程之间传值
开发语言·c++·算法
ai小鬼头9 天前
AIStarter开发者熊哥分享|低成本部署AI项目的实战经验
后端·算法·架构
小白菜3336669 天前
DAY 37 早停策略和模型权重的保存
人工智能·深度学习·算法
zeroporn9 天前
以玄幻小说方式打开深度学习词嵌入算法!! 使用Skip-gram来完成 Word2Vec 词嵌入(Embedding)
人工智能·深度学习·算法·自然语言处理·embedding·word2vec·skip-gram
亮亮爱刷题9 天前
飞往大厂梦之算法提升-7
数据结构·算法·leetcode·动态规划
_周游9 天前
【数据结构】_二叉树OJ第二弹(返回数组的遍历专题)
数据结构·算法