Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting SciTE排序选择工具:带前导空格的数字未按预期排序_Sorting_Lua_Selection_Lines_Scite - Fatal编程技术网

Sorting SciTE排序选择工具:带前导空格的数字未按预期排序

Sorting SciTE排序选择工具:带前导空格的数字未按预期排序,sorting,lua,selection,lines,scite,Sorting,Lua,Selection,Lines,Scite,SciTE编辑器附带嵌入式Lua脚本引擎,可以访问编辑器的文本缓冲区。这使得通过在Lua中编程并从tools(工具)菜单启动的工具来扩展SciTE的功能成为可能。此处提供了一个此类工具: 是按字母顺序对选定行进行排序的工具。 令我恼火的是,它不按数字顺序对包含数字的行进行排序,但如下所示: 1 111 2 222 3 333 我希望: 1 2 3 111 222 333 Google和Co.在这方面帮不了什么忙,因为据

SciTE编辑器附带嵌入式Lua脚本引擎,可以访问编辑器的文本缓冲区。这使得通过在Lua中编程并从tools(工具)菜单启动的工具来扩展SciTE的功能成为可能。此处提供了一个此类工具:

是按字母顺序对选定行进行排序的工具。 令我恼火的是,它不按数字顺序对包含数字的行进行排序,但如下所示:

    1
  111
    2
  222
    3
  333
我希望:

    1
    2
    3
  111
  222
  333

Google和Co.在这方面帮不了什么忙,因为据我所知,这个问题还没有在线解决方案。查找和深入理解table.sort()的Lua文档也不是那么容易。因此,对于一个知识渊博的Lua程序员来说,问题是,修补现有Lua脚本代码的最佳方法是什么,这样数字(以及前导空格中带有文本的行)就可以按预期排序,并且此任务的Lua代码运行得如此之快,甚至可以对大型文件(50 MB及以上)进行排序不会花很多时间吗?

你的期望是错误的。你说过算法应该按字母顺序对文本进行排序,而这正是它所做的

对于Lua,“11”小于“2”。 我想你会同意“aa”应该出现在“b”之前,这几乎是同一回事

如果你想改变文本的排序方式,你必须提供你自己的功能

Lua参考手册说明:

table.sort(列表[,comp])

按给定顺序对列表元素进行排序,从列表[1]到 列表。如果给定了comp,那么它必须是 接收两个列表元素,当第一个元素 必须在最终顺序中的第二个之前(以便 排序,i 注意comp函数必须定义一个严格的偏序 清单中的要素;也就是说,它必须是不对称的,并且 及物的否则,可能无法进行有效排序

排序算法不稳定:元素被 给定的顺序可能会因排序而改变其相对位置

因此,您可以自由实现自己的comp函数来更改排序

默认情况下,
table.sort(list)
按升序排序列表。 要使其按降序排序,请调用:

table.sort(list, function(a,b) return a > b end)
如果你想以不同的方式对待数字,你可以这样做:

t = {"111", "11", "3", "2", "a", "b"}

local function myCompare(a,b)
    local a_number = tonumber(a)
    local b_number = tonumber(b)
    if a_number and b_number then
       return a_number < b_number
    end
end

table.sort(t, myCompare)

for i,v in ipairs(t) do
    print(v)
end

当然,这只是一个简单的例子。更好的实现取决于您。

您的期望是错误的。你说过算法应该按字母顺序对文本进行排序,而这正是它所做的

对于Lua,“11”小于“2”。 我想你会同意“aa”应该出现在“b”之前,这几乎是同一回事

如果你想改变文本的排序方式,你必须提供你自己的功能

Lua参考手册说明:

table.sort(列表[,comp])

按给定顺序对列表元素进行排序,从列表[1]到 列表。如果给定了comp,那么它必须是 接收两个列表元素,当第一个元素 必须在最终顺序中的第二个之前(以便 排序,i 注意comp函数必须定义一个严格的偏序 清单中的要素;也就是说,它必须是不对称的,并且 及物的否则,可能无法进行有效排序

排序算法不稳定:元素被 给定的顺序可能会因排序而改变其相对位置

因此,您可以自由实现自己的comp函数来更改排序

默认情况下,
table.sort(list)
按升序排序列表。 要使其按降序排序,请调用:

table.sort(list, function(a,b) return a > b end)
如果你想以不同的方式对待数字,你可以这样做:

t = {"111", "11", "3", "2", "a", "b"}

local function myCompare(a,b)
    local a_number = tonumber(a)
    local b_number = tonumber(b)
    if a_number and b_number then
       return a_number < b_number
    end
end

table.sort(t, myCompare)

for i,v in ipairs(t) do
    print(v)
end

当然,这只是一个简单的例子。更好的实现取决于您。

下面是我最终想到的。这肯定是一种既快又脏的解决方案,可以减缓已经很慢的速度

(与jEdit[Plugins]->[Text Tools]->[Sort line]或bash命令行'Sort-g'相比)

对文本行的巨大缓冲区进行排序的过程,但它至少是可以使用的,并且按照预期工作。为了完整起见,这里我的Lua启动脚本中当前包含的整个代码部分用于SciTE:

-- =============================================================================
-- Sort Selected Lines (available in MENU -> Tools):
-- -----------------------------------------------------------
-- Specify in .SciTEUser.properties:
--     command.name.2.*=# Sort Selected Lines    '
--     command.subsystem.2.*=3
--     command.mode.2.*=savebefore:no
--     command.2.*=SortSelectedLines
--     # command.shortcut.2.*=Ctrl+2 # Ctrl+2 is DEFAULT for command.2.*

function lines(str)
  local t = {}
  local i, lstr = 1, #str
  while i <= lstr do
    local x, y = string.find(str, "\r?\n", i)
    if x then t[#t + 1] = string.sub(str, i, x - 1)
    else break
    end
    i = y + 1
  end
  if i <= lstr then t[#t + 1] = string.sub(str, i) end
  return t
end

-- It was an annoying for me that using table.sort(buffer) in Lua  
-- didn't sort numbers with leading spaces in their numerical order. 
-- Using following comparison function helps to avoid that problem: 
function compare(a,b)
  return a:gsub(" ", "0") < b:gsub(" ", "0")
-- If 'compare' is not used ( table.sort(buf) )
-- Lua uses implicit for sorting (see Lua tutorial): 
--   return a < b
-- so changing the provided return statement to this above
-- would be enough to restore sorting to how it was before 
end

function SortSelectedLines()
  local sel = editor:GetSelText()
  if #sel == 0 then return end
  local eol = string.match(sel, "\n$")
  local buf = lines(sel)
  table.sort(buf, compare)
--table.foreach (buf, print) --used for debugging
  local out = table.concat(buf, "\n")
  if eol then out = out.."\n" end
  editor:ReplaceSel(out)
end

--  ---------
-- :Sort Selected Lines
-- -----------------------------------------------------------------------------
——=============================================================================
--对所选行排序(在菜单->工具中提供):
-- -----------------------------------------------------------
--在.SciTEUser.properties中指定:
--command.name.2.*=#对所选行排序'
--command.subsystem.2.*=3
--command.mode.2.*=savebefore:否
--command.2.*=SortSelectedLines
--#command.shortcut.2.*=Ctrl+2#Ctrl+2是command.2的默认值*
功能线(str)
局部t={}
局部i,lstr=1,#str

而我低于我最终得出的结论。这肯定是一种既快又脏的解决方案,可以减缓已经很慢的速度

(与jEdit[Plugins]->[Text Tools]->[Sort line]或bash命令行'Sort-g'相比)

对文本行的巨大缓冲区进行排序的过程,但它至少是可以使用的,并且按照预期工作。为了完整起见,这里我的Lua启动脚本中当前包含的整个代码部分用于SciTE:

-- =============================================================================
-- Sort Selected Lines (available in MENU -> Tools):
-- -----------------------------------------------------------
-- Specify in .SciTEUser.properties:
--     command.name.2.*=# Sort Selected Lines    '
--     command.subsystem.2.*=3
--     command.mode.2.*=savebefore:no
--     command.2.*=SortSelectedLines
--     # command.shortcut.2.*=Ctrl+2 # Ctrl+2 is DEFAULT for command.2.*

function lines(str)
  local t = {}
  local i, lstr = 1, #str
  while i <= lstr do
    local x, y = string.find(str, "\r?\n", i)
    if x then t[#t + 1] = string.sub(str, i, x - 1)
    else break
    end
    i = y + 1
  end
  if i <= lstr then t[#t + 1] = string.sub(str, i) end
  return t
end

-- It was an annoying for me that using table.sort(buffer) in Lua  
-- didn't sort numbers with leading spaces in their numerical order. 
-- Using following comparison function helps to avoid that problem: 
function compare(a,b)
  return a:gsub(" ", "0") < b:gsub(" ", "0")
-- If 'compare' is not used ( table.sort(buf) )
-- Lua uses implicit for sorting (see Lua tutorial): 
--   return a < b
-- so changing the provided return statement to this above
-- would be enough to restore sorting to how it was before 
end

function SortSelectedLines()
  local sel = editor:GetSelText()
  if #sel == 0 then return end
  local eol = string.match(sel, "\n$")
  local buf = lines(sel)
  table.sort(buf, compare)
--table.foreach (buf, print) --used for debugging
  local out = table.concat(buf, "\n")
  if eol then out = out.."\n" end
  editor:ReplaceSel(out)
end

--  ---------
-- :Sort Selected Lines
-- -----------------------------------------------------------------------------
——=============================================================================
--对所选行排序(在菜单->工具中提供):
-- -----------------------------------------------------------
--在.SciTEUser.properties中指定:
--command.name.2.*=#对所选行排序'
--指挥子系统