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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天涯过客TYGK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值