lua 模拟Treemap 排序

2018-02-06  本文已影响60人  上重楼

因为项目需要请求参数进行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

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

上一篇 下一篇

猜你喜欢

热点阅读