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
相关推荐
大江东去浪淘尽千古风流人物39 分钟前
【VLN】VLN(Vision-and-Language Navigation视觉语言导航)算法本质,范式难点及解决方向(1)
人工智能·python·算法
努力学算法的蒟蒻1 小时前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
2401_841495641 小时前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
AC赳赳老秦1 小时前
2026国产算力新周期:DeepSeek实战适配英伟达H200,引领大模型训练效率跃升
大数据·前端·人工智能·算法·tidb·memcache·deepseek
2401_841495642 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
budingxiaomoli2 小时前
优选算法-字符串
算法
qq7422349842 小时前
APS系统与OR-Tools完全指南:智能排产与优化算法实战解析
人工智能·算法·工业·aps·排程
A尘埃3 小时前
超市购物篮关联分析与货架优化(Apriori算法)
算法
.小墨迹3 小时前
apollo学习之借道超车的速度规划
linux·c++·学习·算法·ubuntu
不穿格子的程序员3 小时前
从零开始刷算法——贪心篇1:跳跃游戏1 + 跳跃游戏2
算法·游戏·贪心