Sort a list of object identifiers: Difference between revisions

→‎{{header|Lua}}: Updated in accordance with modified task specification
(→‎{{header|Lua}}: Updated in accordance with modified task specification)
Line 206:
 
=={{header|Lua}}==
 
{{update|Lua|the format description and test-case in the task description have been updated}}
 
Using the in-built table.sort with a custom compare function.
<lang Lua>local OIDs = {
".1.3.6.1.4.1.11.2.17.19.3.4.0.10",
".1.3.6.1.4.1.1115011.32.417.05.2.0.79",
".1.3.6.1.4.1.11.2.17.19.3.4.0.194",
".1.3.6.1.4.1.11150.3.4.0.1",
".1.3.6.1.4.1.11.2.17.19.3.4.0.221",
".1.3.6.1.4.1.11.2.17.1911150.3.4.0.2",
".1.3.6.1.4.1.11150.3.4.0.11",
".1.3.6.1.4.1.11.2.17.19.3.4.0.1",
".1.3.6.1.4.1.11.2.17.3773.0.2",
".1.3.6.1.4.1.11.2.17.19.2.0.79",
".1.3.6.1.4.1.11150.3.4.0.21",
".1.3.6.1.4.1.11.2.17.19.2.0.9",
".1.3.6.1.4.1.11.2.17.19.3.4.0.25",
".1.3.6.1.4.1.11.2.17.19.3.4.0.32",
".1.3.6.1.4.1.11.2.17.19.3.4.0.4",
".1.3.6.1.4.1.11.2.17.19.3.4.0.31",
".1.3.6.1.4.1.11.2.17.19.3.4.0.3",
".1.3.6.1.4.1.11.2.17.3773.0.1"
}
 
function compare (a, b)
local aList, bList, Na, Nb = {}, {}
Line 236 ⟶ 221:
for num in b:gmatch("%d+") do table.insert(bList, num) end
for i = 1, math.max(#aList, #bList) do
Na, Nb = tonumber(aList[i]) or 0, tonumber(bList[i]) or 0
if Na ~= Nb then return Na < Nb end
end
end
 
table.sort(OIDs, compare)
table.foreachfor _, oid in pairs(OIDs,) do print(oid) end</lang>
{{out}}
<pre>1 .1.3.6.1.4.1.11.2.17.195.2.0.979
2 .1.3.6.1.4.1.11.2.17.19.23.4.0.791
3 .1.3.6.1.4.1.11.2.17.19.3.4.0.14
4 .1.3.6.1.4.1.11.2.17.19.3.4.0.210
5 .1.3.6.1.4.1.11.2.17.1911150.3.4.0.3
6 .1.3.6.1.4.1.11.2.17.1911150.3.4.0.41</pre>
7 .1.3.6.1.4.1.11.2.17.19.3.4.0.10
8 .1.3.6.1.4.1.11.2.17.19.3.4.0.19
9 .1.3.6.1.4.1.11.2.17.19.3.4.0.22
10 .1.3.6.1.4.1.11.2.17.19.3.4.0.25
11 .1.3.6.1.4.1.11.2.17.19.3.4.0.31
12 .1.3.6.1.4.1.11.2.17.19.3.4.0.32
13 .1.3.6.1.4.1.11.2.17.3773.0.1
14 .1.3.6.1.4.1.11.2.17.3773.0.2
15 .1.3.6.1.4.1.11150.3.4.0.1
16 .1.3.6.1.4.1.11150.3.4.0.2
17 .1.3.6.1.4.1.11150.3.4.0.11
18 .1.3.6.1.4.1.11150.3.4.0.21</pre>
 
=={{header|Perl}}==
Anonymous user