Add pyramid script to sort lines by length
This commit is contained in:
parent
20dd449a03
commit
e802e65c7f
1 changed files with 27 additions and 0 deletions
27
bin/pyramid
Executable file
27
bin/pyramid
Executable file
|
@ -0,0 +1,27 @@
|
||||||
|
#!/usr/bin/env lua
|
||||||
|
|
||||||
|
local arrr = require 'arrr'
|
||||||
|
|
||||||
|
local options = arrr {
|
||||||
|
{ "Sort longest lines first", "--reverse", "-r" };
|
||||||
|
} {...}
|
||||||
|
|
||||||
|
local lines = {}
|
||||||
|
|
||||||
|
for line in io.lines() do
|
||||||
|
table.insert(lines, {#line, line})
|
||||||
|
end
|
||||||
|
|
||||||
|
if options.reverse then
|
||||||
|
table.sort(lines, function(a, b)
|
||||||
|
return a[1] > b[1]
|
||||||
|
end)
|
||||||
|
else
|
||||||
|
table.sort(lines, function(a, b)
|
||||||
|
return a[1] < b[1]
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
for i, line in ipairs(lines) do
|
||||||
|
print(line[2])
|
||||||
|
end
|
Loading…
Reference in a new issue