如何根据另一个字符串表定义的顺序对字符串表进行排序(Lua)

Posted

技术标签:

【中文标题】如何根据另一个字符串表定义的顺序对字符串表进行排序(Lua)【英文标题】:How to Sort a table of strings according to the order defined by another table of strings (Lua) 【发布时间】:2021-12-20 20:05:48 【问题描述】:

我有 2 个 lua 表:

OrderTbl = 'Hello', 'Question', 'Answer', 'Bye'
UnsortedTbl = 'Question', 'Bye, 'Bye', 'Question', 'Hello', 'Something'

如何按照 OrderTbl 给出的顺序对 UnsortedTbl 进行排序? (OrderTbl中没有找到的字段放在结果表的末尾,未排序)

我已经从 Java 翻译了一个代码示例,它可以与 numbers 一起使用。这里是:

function first(arr, low, high, x, n)
    if high >= low then
    
        -- (low + high)/2
        local mid = low + math.floor((high - low) / 2)
        
        if (mid == 1 or x > arr[mid - 1]) and arr[mid] == x then
            return mid
        end
        if x > arr[mid] then return first(arr, (mid + 1), high, x, n) end
        return first(arr, low, (mid - 1), x, n)
    end
    return nil
end

-- Sort A1 according to the order
-- defined by A2
function sortAccording(A1, A2)
    local m=#A1
    local n=#A2

    -- The temp array is used to store a copy
    -- of A1 and visited is used to mark the
    -- visited elements in temp.
    local temp = 
    local visited = 

    for i = 1, m do
        temp[i] = A1[i]
        visited[i] = 0
    end

    -- Sort elements in temp
    table.sort(temp)

    -- for index of output which is sorted A1
    local ind = 0

    -- Consider all elements of A2, find them
    -- in temp and copy to A1 in order.
    for i = 1, n do
        -- Find index of the first occurrence
        -- of A2[i] in temp
        local f = first(temp, 1, m, A2[i], m+1)
        -- If not present, no need to proceed
        if not f then
            -- continue
        else
            -- Copy all occurrences of A2[i] to A1
            j = f
            while j < m and temp[j] == A2[i] do
                A1[ind] = temp[j]
                ind = ind + 1
                visited[j] = 1
                j = j + 1
            end
        end
    end
    -- Now copy all items of temp which are
    -- not present in A2
    for i = 1, m do
        if visited[i] == 0 then
            ind = ind + 1
            A1[ind] = temp[i]
        end
    end

end

function printArray(arr)
    for i = 1, #arr do 
        print(arr[i] .. " ") 
    end
end

-- Driver program to test above function.
local A1 = 2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8
local A2 = 2, 1, 4, 3, 6, 5, 8, 7

sortAccording(A1, A2)
printArray(A1)

我不太明白如何使它与 strings 一起工作。你能帮帮我吗?

【问题讨论】:

如果您阅读手册,您会看到table.sort 带有一个可选的第二个参数。你知道它有什么用吗? 是的,我已经在学习了:) 【参考方案1】:

您可以使用接受自定义比较器的table.sort 形式:

local OrderTbl = 'Hello', 'Question', 'Answer', 'Bye'
local UnsortedTbl = 'Question', 'Bye', 'Bye', 'Question', 'Hello', 'Something', 'Else'

-- convert the order to hash that can be easily queried
for idx, val in ipairs(OrderTbl) do OrderTbl[val] = idx end

local maxIdx = #OrderTbl + 1 -- this will mark "missing" elements
-- pass a custom comparator that will check OrderTbl
table.sort(UnsortedTbl, function(a, b)
    local pa = OrderTbl[a] or maxIdx -- desired index of a
    local pb = OrderTbl[b] or maxIdx -- desired index of b
    if pa == pb then return a < b end -- sort by name
    return pa < pb -- sort by index
  end)

【讨论】:

以上是关于如何根据另一个字符串表定义的顺序对字符串表进行排序(Lua)的主要内容,如果未能解决你的问题,请参考以下文章

根据对另一个 NSArray 字符串的排序,对自定义对象的 NSArray 进行排序

如何使用另一个列表中的项目顺序对字符串列表进行排序?

excel:根据另一列中的顺序对十进制值列表进行排序

如何按字母顺序对 DBML 对象进行排序?

如何根据另一个数组的顺序对对象数组进行排序?

按自定义字典顺序对字符串进行排序