Lua按字母顺序对表进行排序,但不包括数字。

我想按字母表顺序对表格进行排序。除了数字以外。

下面的代码显示了如何使用比较函数对表进行排序:

function( a,b ) return a.N < b.N end

它给我带来了以下结果:

obj = {
    [1] = {
        ["N"] = "Green 1";
    };
    [2] = {
        ["N"] = "Green 11";
    };
    [3] = {
        ["N"] = "Green 2";
    };
    [4] = {
        ["N"] = "Red 1";
    };
}

但我希望它排序如下:

obj = {
    [1] = {
        ["N"] = "Green 1";
    };
    [2] = {
        ["N"] = "Green 2";
    };
    [3] = {
        ["N"] = "Green 11";
    };
    [4] = {
        ["N"] = "Red 1";
    };
}

是否可能呢?

点赞
用户107090
用户107090

尝试一下:

local function split(a)
    local x,y=a.N:match("(%S+)%s+(%S+)")
    return x,tonumber(y)
end

table.sort(obj,
    function (a,b)
        local a1,a2=split(a)
        local b1,b2=split(b)
        return a1<b1 or (a1==b1 and a2<b2)
    end
)
2015-01-12 20:30:03
用户1442917
用户1442917

@lhf的解决方案应该适合您,但是您可能需要考虑一些角落情况,比如将"Green 1"与"Green 02"进行比较或将"Green 2"与"Green 02"进行比较。我在一篇博客文章中评论了几种实现字母数字排序的方法,并比较了它们的结果。您还可以在lua邮件列表上的讨论中查看关于此话题的讨论。

2015-01-12 20:41:03
用户415823
用户415823

原本打算发布这个问题,但是 lhf 给出的解决方案已经回答了你的问题。既然你还遇到了问题,那就试试下面的方案吧。

local function cmp(a, b)
   a = tostring(a.N)
   b = tostring(b.N)
   local patt = '^(.-)%s*(%d+)$'
   local _,_, col1, num1 = a:find(patt)
   local _,_, col2, num2 = b:find(patt)
   if (col1 and col2) and col1 == col2 then
      return tonumber(num1) < tonumber(num2)
   end
   return a < b
end

local obj = {
   { N = '1'           },
   { N = 'Green1'      }, -- works with optional space
   { N = 'Green'       }, -- works when doesn't fit the format
   { N = 'Sky blue99'  },
   { N = 'Green 11'    },
   { N = 'Green 2'     },
   { N = 'Red 02'      }, -- works when has leading zeros
   { N = 'Red    01'   }, -- works with padding spaces
   { N = 'Sky blue 42' }, -- works with multi-word color names
   { N = 99            }, -- works with numbers
}

table.sort(obj, cmp)
for i,v in ipairs(obj) do
   print(i, v.N)
end

输出:

1   1
2   99
3   Green
4   Green1
5   Green 2
6   Green 11
7   Red    01
8   Red 02
9   Sky blue 42
10  Sky blue99
2015-01-12 22:13:27