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
相关推荐
CoovallyAIHub2 小时前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub3 小时前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
聚客AI20 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v1 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工1 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
moonlifesudo1 天前
322:零钱兑换(三种方法)
算法
NAGNIP2 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队2 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法