lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Just hacked this up on a whim:


function L(c)
    return assert(loadstring(
        "local args={...}; return " .. c:gsub('%$(%d+)', 'args[%1]')
    ))
end

local t={'bananas', 'watermelons', 'pears', 'grapes'}
table.sort(t, L"$1 > $2")
for k,v in pairs(t) do print(k,v) end


Maybe it's not the most elegant (hope your function never uses a $ in a string) but I thought it was a neat little hack and a bit nicer looking than:
table.sort(t, function(a, b) return a > b end)

--
Sent from my Game Boy.