Sorting 在Lua中连接各种表

Sorting 在Lua中连接各种表,sorting,loops,lua,Sorting,Loops,Lua,我已经寻找这个问题的答案好几天了,我设法用了一个技巧,省略了这个连接部分,只使用了几个单独的循环将不同的值重新插入到同一个表中。。。 但我的问题是 默认情况下,table.sort使用

我已经寻找这个问题的答案好几天了,我设法用了一个技巧,省略了这个连接部分,只使用了几个单独的循环将不同的值重新插入到同一个表中。。。 但我的问题是

默认情况下,table.sort使用<来比较数组元素,因此它可以 仅对数字数组或字符串数组进行排序。写一个比较 函数,该函数允许table.sort对混合类型的数组进行排序。在 排序数组中,应将给定类型的所有值分组在一起。 在每个这样的组中,数字和字符串应按常规进行排序, 其他类型应该以某种任意但一致的方式进行排序

正如我所说,我使用不同的for循环解决了这个问题,但是有没有一种方法可以只分析表中的每个元素,然后将其分配给不同的表???比如:“表,函数,Nums,字符串,…”,然后在分析完成后,将它们连接在一起,得到排序版本中的同一个表

我对此低效的回答是:

function Sep(val)
local NewA = {}

   for i in pairs(val) do
      if type(val[i]) == "string" then
     table.insert(NewA,val[i])
      end
   end
for i in pairs(val) do
      if type(val[i]) == "number" then
     table.insert(NewA,val[i])
      end
   end

for i in pairs(val) do
      if type(val[i]) == "function" then
     table.insert(NewA,tostring(val[i]))
      end
   end

for i in pairs(val) do
      if type(val[i]) == "table" then
     table.insert(NewA,tostring(val[i]))
      end
   end

for i in pairs(val) do
      if type(val[i]) == "boolean" then
     table.insert(NewA,tostring(val[i]))
      end
   end

for i in pairs(NewA) do
   print(NewA[i])
end
end

据我所知,您希望先按类型排序,然后按值排序: 您可以编写自己的自定义谓词并将其传递给sort

-- there would be need of custom < function since tables cannot be compared
-- you can overload operator < as well I suppose.
 function my_less (lhs, rhs)
    if (type (lhs) ~= "number" or type (lhs) ~= "string") then
        return tostring (lhs) < tostring (rhs) 
    else
        return lhs < rhs;
    end;
 end;

 -- the custom predicate I mentioned
 function sort_predicate (a,b)
    -- if the same type - compare variable, else compare types
    return (type (a) == type (b) and my_less (a, b)) or type (a) < type (b);
 end

 table.sort (A, sort_predicate);
——由于无法比较表格,因此需要自定义<函数
--我想你也可以重载操作符<。
功能my_less(左、右)
如果(类型(lhs)~=“数字”或类型(lhs)~=“字符串”),则
返回到管柱(左侧)<返回到管柱(右侧)
其他的
返回左侧<右侧;
结束;
结束;
--我提到的自定义谓词
函数sort_谓词(a,b)
--如果相同类型-比较变量,则比较其他类型
返回(类型(a)=类型(b)和类型(a,b))或类型(a)<类型(b);
结束
table.sort(A,sort\u谓词);

[根据@tozka的评论更新]

你可以把整个事情归结为4行。您需要做的就是检查类型(lhs)==“number”,如果是,则返回lhs>我没有得到将两个或多个不同的表值连接在一起并将它们添加到一个包含所有值的表中的方法的答案。这不会按照正确的顺序对数字进行排序
a={1,2,21,12}
将给出
1,12,2,21
@tozka,更新了一个稍微复杂一点的版本来处理这个问题。现在你几乎遇到了oposite问题,而
a={2,12}
将可以排序(因为我们正在排序数字)
A={“2”,“12”}
(注意,现在我们对字符串而不是数字进行排序)将给出
{“2”,“12”}
,其中字符串的正确排序顺序是
{“12”,“2”}
正确;我认为这样更“自然”;)@选民们,如果能对你不同意的观点发表评论,那将是一件好事。风格物质?格式化?
-- there would be need of custom < function since tables cannot be compared
-- you can overload operator < as well I suppose.
 function my_less (lhs, rhs)
    if (type (lhs) ~= "number" or type (lhs) ~= "string") then
        return tostring (lhs) < tostring (rhs) 
    else
        return lhs < rhs;
    end;
 end;

 -- the custom predicate I mentioned
 function sort_predicate (a,b)
    -- if the same type - compare variable, else compare types
    return (type (a) == type (b) and my_less (a, b)) or type (a) < type (b);
 end

 table.sort (A, sort_predicate);
A = { {}, {}, {}, "", "a", "b", "c", "2", "12", 1, 2, 3, -100, 1.1, 12, 11,
  function() end, function() end, false, false, true }

table.sort(A, function(a,b)
  if type(a) == type(b) and type(a) == 'number' then return a < b end
  return type(a)..tostring(a) < type(b)..tostring(b) end)
{false, false, true, function() end, function() end,
 -100, 1, 1.1, 2, 3, 11, 12, "", "12", "2", "a", "b", "c", {}, {}, {}}