ByTransient Expert Cheater Reputation: 5
Joined: 05 Sep 2020 Posts: 240
|
Posted: Tue Mar 23, 2021 7:19 am Post subject: List from small to large, filter (table.sort)[Solved] |
|
|
Hello there.
An example code is found below.
How do I list the list below from smallest to largest?
Code: | local pn1=[[item_aaaaaaaaaaa
item_bbbbbbbbbbbbbbbbbbbbbbbb
item_ccccccccccccccccc
item_aaaaaaaaaaaaaa
item_bbbbbbbbbbbbbbbbbbbbbbbbbb
item_cccccccccccccccccccc
item_aaaaaaa
item_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
item_ccccccccccccccc
item_aaaaaaaaaaaaaaaaa
item_bbbbbbbbbbbbbbbbbbbbb
item_ccccccccccccccccc
item_aaaaaaaaaaaaaaaaaaaaa
item_bbbbbbbbbbbbbbbbbbbbbbbb
item_ccccccccccccccccccccccccccc
item_aaaaaaaaa]]
function filter()
local SL = createStringlist()
SL.Text=pn1
for i=0, strings_getCount(SL)-1 do
local item1 = SL[i]
local item2 = SL[i]:sub(1, 6)
--print(item2)
local itemLeng = # (item1)
if item2=="item_a" then
print(itemLeng.." - "..item1)
end
end
SL.destroy()
end
filter() |
result;
Code: | 16 - item_aaaaaaaaaaa
19 - item_aaaaaaaaaaaaaa
12 - item_aaaaaaa
22 - item_aaaaaaaaaaaaaaaaa
26 - item_aaaaaaaaaaaaaaaaaaaaa
14 - item_aaaaaaaaa |
The list I want;
Code: | 12 - item_aaaaaaa
14 - item_aaaaaaaaa
16 - item_aaaaaaaaaaa
19 - item_aaaaaaaaaaaaaa
22 - item_aaaaaaaaaaaaaaaaa
26 - item_aaaaaaaaaaaaaaaaaaaaa |
Probably "table.sort" should be used.
However, it confuses me.
Is it possible to get a short sample?
-----------------------------------------------------------
-----------------------------------------------------------
[ EDIT ]
Thanks @Corroder.
I followed the traces you left.
Here's the simple sort method for those who need it.
Code: | function filter()
local SL = createStringlist()
SL.Text=pn1
UDF1.CEMemo2.Lines.Add("")
for i=0, strings_getCount(SL)-1 do
local item1 = SL[i]
local item2 = SL[i]:sub(1, 6)
local itemLeng = # (item1)
if item2=="item_a" then
UDF1.CEMemo2.Lines.Add(itemLeng.." - "..item1)
end
end
SL = nil
end
function GetTheTableList()
local T = {}
local SL=createStringlist()
SL.Text=UDF1.CEMemo1.Lines.Text
for i=0,strings_getCount(SL)-1 do
T[i] = strings_getString(SL,i)
--print("T - "..T[i]) -- Output
end
SL = nil
return T
end
function SortByTable(sender)
Tablesort = {}
for x = 0, #sender do
Tablesort[x] = sender[x]
end
table.sort(Tablesort)
return Tablesort --- retunr new table preserving old table
end
function UDF1_CEButton1Click(sender)
filter()
end
function UDF1_CEButton2Click(sender)
tbl2 = GetTheTableList()
Sort = SortByTable(tbl2)
for x = 0, #Sort do
--print(Sort[x])
UDF1.CEMemo3.Lines.Add(Sort[x])
end
end |
|
|