lua 模拟Treemap 排序

因为项目需要请求参数进行treemap自然排序进行参数校验,前端很好搞 我这做在openresty上的lua校验就比较麻烦了,也没搜到解决方案,就自己尝试弄了下

-- 类Treemap自然排序函数
local function list_sort(a, b)
    local a_len = string.len(a)
    local b_len = string.len(b)
    local max_len = a_len
    if b_len > a_len then
        max_len = b_len
    end



    for i = 1, max_len do
        if string.len(a) < i then
            if a_len > b_len then
                return false
            else
                return true
            end
        end
        if string.len(b) < i then
            if a_len > b_len then
                return false
            else
                return true
            end
        end

        local a_byte = string.byte(a, i)
        local b_byte = string.byte(b, i)

        if a_byte == b_byte then

        end
        if a_byte > b_byte then
            return false
        end
        if a_byte < b_byte then
            return true
        end
    end
end

param_key_list = {  "province","city","provinceCode","areaCode", "integrateStoreId", "detailAddress", "appKey",  "cityCode","receiveName", "area",   "subShopName" }

table.sort(param_key_list, list_sort)
for i, k in ipairs(param_key_list) do
    print(k)
end

目前在项目使用没遇到问题

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容