Sort a list of object identifiers: Difference between revisions
No edit summary |
(Added Elixir) |
||
Line 54: | Line 54: | ||
# 17 | .1.3.6.1.4.1.11150.3.4.0.11 | |
# 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> |
# 18 | .1.3.6.1.4.1.11150.3.4.0.21 |</pre> |
||
=={{header|Elixir}}== |
|||
{{works with|Elixir|1.3.1}} |
|||
<lang elixir>defmodule Sort_by_OID do |
|||
def numbers(list) do |
|||
Enum.sort_by(list, fn oid -> |
|||
String.split(oid, ".") |> Enum.map(&String.to_integer("0"<>&1)) |
|||
end) |
|||
end |
|||
end |
|||
listOID = ~w[ |
|||
.1.3.6.1.4.1.11.2.17.19.3.4.0.10 |
|||
.1.3.6.1.4.1.11150.3.4.0.2 |
|||
.1.3.6.1.4.1.11.2.17.19.3.4.0.19 |
|||
.1.3.6.1.4.1.11150.3.4.0.1 |
|||
.1.3.6.1.4.1.11.2.17.19.3.4.0.22 |
|||
.1.3.6.1.4.1.11.2.17.19.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 |
|||
] |
|||
Sort_by_OID.numbers(listOID) |
|||
|> Enum.with_index |
|||
|> Enum.each(fn {oid,i} -> |
|||
:io.fwrite "# ~2..0w | ~s |~n", [i+1, oid] |
|||
end)</lang> |
|||
{{out}} |
|||
<pre> |
|||
# 01 | .1.3.6.1.4.1.11.2.17.19.2.0.9 | |
|||
# 02 | .1.3.6.1.4.1.11.2.17.19.2.0.79 | |
|||
# 03 | .1.3.6.1.4.1.11.2.17.19.3.4.0.1 | |
|||
# 04 | .1.3.6.1.4.1.11.2.17.19.3.4.0.2 | |
|||
# 05 | .1.3.6.1.4.1.11.2.17.19.3.4.0.3 | |
|||
# 06 | .1.3.6.1.4.1.11.2.17.19.3.4.0.4 | |
|||
# 07 | .1.3.6.1.4.1.11.2.17.19.3.4.0.10 | |
|||
# 08 | .1.3.6.1.4.1.11.2.17.19.3.4.0.19 | |
|||
# 09 | .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> |
|||
Revision as of 19:13, 15 July 2016
Sort by OID numbers.
OID - Object Identifiers are used in Network data These numbers can vary in length, and have a dot "." between them. The individual number can be of different number of digits also.
Example:
====================================== INPUT @arrayOID = ( ".1.3.6.1.4.1.11.2.17.19.3.4.0.10", ".1.3.6.1.4.1.11150.3.4.0.2", ".1.3.6.1.4.1.11.2.17.19.3.4.0.19", ".1.3.6.1.4.1.11150.3.4.0.1", ".1.3.6.1.4.1.11.2.17.19.3.4.0.22", ".1.3.6.1.4.1.11.2.17.19.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" ); ======================================== OUTPUT - Correctly Sorted # 01 | .1.3.6.1.4.1.11.2.17.19.2.0.9 | # 02 | .1.3.6.1.4.1.11.2.17.19.2.0.79 | # 03 | .1.3.6.1.4.1.11.2.17.19.3.4.0.1 | # 04 | .1.3.6.1.4.1.11.2.17.19.3.4.0.2 | # 05 | .1.3.6.1.4.1.11.2.17.19.3.4.0.3 | # 06 | .1.3.6.1.4.1.11.2.17.19.3.4.0.4 | # 07 | .1.3.6.1.4.1.11.2.17.19.3.4.0.10 | # 08 | .1.3.6.1.4.1.11.2.17.19.3.4.0.19 | # 09 | .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 |
Elixir
<lang elixir>defmodule Sort_by_OID do
def numbers(list) do Enum.sort_by(list, fn oid -> String.split(oid, ".") |> Enum.map(&String.to_integer("0"<>&1)) end) end
end
listOID = ~w[
.1.3.6.1.4.1.11.2.17.19.3.4.0.10 .1.3.6.1.4.1.11150.3.4.0.2 .1.3.6.1.4.1.11.2.17.19.3.4.0.19 .1.3.6.1.4.1.11150.3.4.0.1 .1.3.6.1.4.1.11.2.17.19.3.4.0.22 .1.3.6.1.4.1.11.2.17.19.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
] Sort_by_OID.numbers(listOID) |> Enum.with_index |> Enum.each(fn {oid,i} ->
:io.fwrite "# ~2..0w | ~s |~n", [i+1, oid] end)</lang>
- Output:
# 01 | .1.3.6.1.4.1.11.2.17.19.2.0.9 | # 02 | .1.3.6.1.4.1.11.2.17.19.2.0.79 | # 03 | .1.3.6.1.4.1.11.2.17.19.3.4.0.1 | # 04 | .1.3.6.1.4.1.11.2.17.19.3.4.0.2 | # 05 | .1.3.6.1.4.1.11.2.17.19.3.4.0.3 | # 06 | .1.3.6.1.4.1.11.2.17.19.3.4.0.4 | # 07 | .1.3.6.1.4.1.11.2.17.19.3.4.0.10 | # 08 | .1.3.6.1.4.1.11.2.17.19.3.4.0.19 | # 09 | .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 |
J
Data:
<lang J>oids=:<@-.&' ';._2]0 :0
.1.3.6.1.4.1.11.2.17.19.3.4.0.10 .1.3.6.1.4.1.11150.3.4.0.2 .1.3.6.1.4.1.11.2.17.19.3.4.0.19 .1.3.6.1.4.1.11150.3.4.0.1 .1.3.6.1.4.1.11.2.17.19.3.4.0.22 .1.3.6.1.4.1.11.2.17.19.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
)</lang>
In other words, for each line in that script, remove the spaces and put the rest in a box.
Sorting:
<lang J> >(/: __&".;._1&.>) oids .1.3.6.1.4.1.11.2.17.19.2.0.9 .1.3.6.1.4.1.11.2.17.19.2.0.79 .1.3.6.1.4.1.11.2.17.19.3.4.0.1 .1.3.6.1.4.1.11.2.17.19.3.4.0.2 .1.3.6.1.4.1.11.2.17.19.3.4.0.3 .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.10 .1.3.6.1.4.1.11.2.17.19.3.4.0.19 .1.3.6.1.4.1.11.2.17.19.3.4.0.22 .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.31 .1.3.6.1.4.1.11.2.17.19.3.4.0.32 .1.3.6.1.4.1.11.2.17.3773.0.1 .1.3.6.1.4.1.11.2.17.3773.0.2 .1.3.6.1.4.1.11150.3.4.0.1 .1.3.6.1.4.1.11150.3.4.0.2 .1.3.6.1.4.1.11150.3.4.0.11 .1.3.6.1.4.1.11150.3.4.0.21 </lang>
In other words, for our sort key, we break the contents of each box by the initial character and treat the remainder as numbers.
We also pull the result out of its boxes for display purposes.
Lua
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.11150.3.4.0.2", ".1.3.6.1.4.1.11.2.17.19.3.4.0.19", ".1.3.6.1.4.1.11150.3.4.0.1", ".1.3.6.1.4.1.11.2.17.19.3.4.0.22", ".1.3.6.1.4.1.11.2.17.19.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 = {}, {} for num in a:gmatch("%d+") do table.insert(aList, num) end 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]), tonumber(bList[i]) if Na ~= Nb then return Na < Nb end end
end
table.sort(OIDs, compare) table.foreach(OIDs, print)</lang>
- Output:
1 .1.3.6.1.4.1.11.2.17.19.2.0.9 2 .1.3.6.1.4.1.11.2.17.19.2.0.79 3 .1.3.6.1.4.1.11.2.17.19.3.4.0.1 4 .1.3.6.1.4.1.11.2.17.19.3.4.0.2 5 .1.3.6.1.4.1.11.2.17.19.3.4.0.3 6 .1.3.6.1.4.1.11.2.17.19.3.4.0.4 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
Perl
<lang perl>
- !/usr/bin/perl
- --------------------------
- Sort: -OID - Numeric
- Bert Mariani 2016-07-07
- --------------------------
@arrayOID = () ; @arrayOIDSorted = () ;
@arrayOID = (
".1.3.6.1.4.1.11.2.17.19.3.4.0.10", ".1.3.6.1.4.1.11150.3.4.0.2", ".1.3.6.1.4.1.11.2.17.19.3.4.0.19", ".1.3.6.1.4.1.11150.3.4.0.1", ".1.3.6.1.4.1.11.2.17.19.3.4.0.22", ".1.3.6.1.4.1.11.2.17.19.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"
);
### One big long line ... made readable @arrayOIDSorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, join , map { sprintf("%8d",$_) } split( /\./, $_) ] } @arrayOID;
print "\nAfter OID Sort \n\n"; $ptrN = 0; $entry = 1;
while ( $arrayOIDSorted[$ptrN] ) { print "$entry | $arrayOIDSorted[$ptrN] |\n" ; $entry++; $ptrN++; }
print "\nEnd OID Sort \n\n";
</lang>
- Output:
1 | .1.3.6.1.4.1.11.2.17.19.2.0.9 | 2 | .1.3.6.1.4.1.11.2.17.19.2.0.79 | 3 | .1.3.6.1.4.1.11.2.17.19.3.4.0.1 | 4 | .1.3.6.1.4.1.11.2.17.19.3.4.0.2 | 5 | .1.3.6.1.4.1.11.2.17.19.3.4.0.3 | 6 | .1.3.6.1.4.1.11.2.17.19.3.4.0.4 | 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 |
Perl 6
<lang perl6>use v6;
.say for sort { $^oid.comb(/\d+/).fmt('%08d') }, <
.1.3.6.1.4.1.11.2.17.19.3.4.0.10 .1.3.6.1.4.1.11150.3.4.0.2 .1.3.6.1.4.1.11.2.17.19.3.4.0.19 .1.3.6.1.4.1.11150.3.4.0.1 .1.3.6.1.4.1.11.2.17.19.3.4.0.22 .1.3.6.1.4.1.11.2.17.19.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
>;</lang>
- Output:
.1.3.6.1.4.1.11.2.17.19.2.0.9 .1.3.6.1.4.1.11.2.17.19.2.0.79 .1.3.6.1.4.1.11.2.17.19.3.4.0.1 .1.3.6.1.4.1.11.2.17.19.3.4.0.2 .1.3.6.1.4.1.11.2.17.19.3.4.0.3 .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.10 .1.3.6.1.4.1.11.2.17.19.3.4.0.19 .1.3.6.1.4.1.11.2.17.19.3.4.0.22 .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.31 .1.3.6.1.4.1.11.2.17.19.3.4.0.32 .1.3.6.1.4.1.11.2.17.3773.0.1 .1.3.6.1.4.1.11.2.17.3773.0.2 .1.3.6.1.4.1.11150.3.4.0.1 .1.3.6.1.4.1.11150.3.4.0.2 .1.3.6.1.4.1.11150.3.4.0.11 .1.3.6.1.4.1.11150.3.4.0.21
Ring
<lang Ring>
/*
+-------------------------------------------------------------- + Program Name : SortOIDNumeric.ring + Date : 2016-07-14 + Author : Bert Mariani + Purpose : Sort OID List in Numeric Order +--------------------------------------------------------------
- /
oldOidList = [
".1.3.6.1.4.1.11150.3.4.0.21", ".1.3.6.1.4.1.11150.3.4.0.11", ".1.3.6.1.4.1.11150.3.4.0.2", ".1.3.6.1.4.1.11150.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.3773.0.1", ".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.31", ".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.22", ".1.3.6.1.4.1.11.2.17.19.3.4.0.19", ".1.3.6.1.4.1.11.2.17.19.3.4.0.10", ".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.3" , ".1.3.6.1.4.1.11.2.17.19.3.4.0.2" , ".1.3.6.1.4.1.11.2.17.19.3.4.0.1" , ".1.3.6.1.4.1.11.2.17.19.2.0.79", ".1.3.6.1.4.1.11.2.17.19.2.0.9" ]
### SHOW BEFORE SORT See nl + "oldOIDList Before Sort" +nl See oldOidList
#---------------------
delChar = "." nulChar = "" padChar = " " padSize = 6 newDotPadList = []
### Split list into lines for line in oldOidList
### Split line by . into components noDotList = str2list( substr(line, delChar, nl) )
### Pad components with left blanks to make equal size newPadList = PadStringList(noDotList, padChar, padSize)
### Join the components back to a line newDotPadString = JoinStringList(delChar, newPadList)
### Create new list - Alpha Add(newDotPadList, newDotPadString) next
### Sorts Alpha list newDotPadListSorted = sort(newDotPadList)
### SHOW ALPHA INTERMEDIATE OUTPUT See nl + "newDotPadListSorted Intermediate Sort" +nl See newDotPadListSorted
### Remove blanks for original look newOidList = RemovePadCharList( newDotPadListSorted, padChar, nulChar)
###--------------------
### SHOW AFTER SORT - NUMERIC See nl + "newOIDList Final Sort" +nl See newOidList
- --------------------------------------------------------------------
- Function: PadStringList
- newList = PadStringList(oldList, padChar, padSize )
- --------------------------------------------------------------------
Func PadStringList oldList, padChar, padSize
newList = [] for line in oldList newPadSize = padSize - len(line) newLine = Copy( padChar, newPadSize) + line Add(newList, newLine) next
### First line in all blank because of leading dot - remove Del(newList,1)
return newList
- ------------------------------------------------------------
- FUNC JoinStringList
- newString = JoinStringList( joinChar, oldList)
- ------------------------------------------------------------
Func JoinStringList joinChar, OldList
newString = "" for line in OldList newString = newString + joinChar + line next
return newString
- ---------------------------------------------------------------------
- FUNC RemovePadCharList
- newOidList = RemovePadCharList( oldList, padChar, nulChar)
- ---------------------------------------------------------------------
Func RemovePadCharList oldList, padChar, nulChar
newList = [] for line in oldList noPadString = substr(line, padChar, nulChar) Add(newList, noPadString) next
return newList
- -----------------------------------------------------------
>;</lang>
- Output:
.1.3.6.1.4.1.11.2.17.19.2.0.9 .1.3.6.1.4.1.11.2.17.19.2.0.79 .1.3.6.1.4.1.11.2.17.19.3.4.0.1 .1.3.6.1.4.1.11.2.17.19.3.4.0.2 .1.3.6.1.4.1.11.2.17.19.3.4.0.3 .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.10 .1.3.6.1.4.1.11.2.17.19.3.4.0.19 .1.3.6.1.4.1.11.2.17.19.3.4.0.22 .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.31 .1.3.6.1.4.1.11.2.17.19.3.4.0.32 .1.3.6.1.4.1.11.2.17.3773.0.1 .1.3.6.1.4.1.11.2.17.3773.0.2 .1.3.6.1.4.1.11150.3.4.0.1 .1.3.6.1.4.1.11150.3.4.0.2 .1.3.6.1.4.1.11150.3.4.0.11 .1.3.6.1.4.1.11150.3.4.0.21
zkl
Translation of http://rosettacode.org/wiki/Natural_sorting#zkl
Basically, blow apart each line into a list of numbers and sort that. <lang zkl>fcn sortOIDS(oids){
oids:=oids.pump(List(),fcn(oid){ oid[1,*].split(".").apply("toInt") }); oids.sort( // in place sort fcn(a,b){ // a&b are (x,y,z,...), eg (0,4,2,54)
a.zip(b).reduce(fcn(_,[(a,b)]){ if(a==b)return(True); // continue to next field return(Void.Stop,a<b); },True);
}); oids.pump(List,fcn(a,b,c,etc){ "."+vm.arglist[0].concat(".") }) // back to strings
}</lang> <lang zkl>oids:=List(
".1.3.6.1.4.1.11.2.17.19.3.4.0.10", ".1.3.6.1.4.1.11150.3.4.0.2", ".1.3.6.1.4.1.11.2.17.19.3.4.0.19", ".1.3.6.1.4.1.11150.3.4.0.1", ".1.3.6.1.4.1.11.2.17.19.3.4.0.22", ".1.3.6.1.4.1.11.2.17.19.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");
sortOIDS(oids).pump(Console.println);</lang>
- Output:
.1.3.6.1.4.1.11.2.17.19.2.0.9 .1.3.6.1.4.1.11.2.17.19.2.0.79 .1.3.6.1.4.1.11.2.17.19.3.4.0.1 .1.3.6.1.4.1.11.2.17.19.3.4.0.2 .1.3.6.1.4.1.11.2.17.19.3.4.0.3 .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.10 .1.3.6.1.4.1.11.2.17.19.3.4.0.19 .1.3.6.1.4.1.11.2.17.19.3.4.0.22 .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.31 .1.3.6.1.4.1.11.2.17.19.3.4.0.32 .1.3.6.1.4.1.11.2.17.3773.0.1 .1.3.6.1.4.1.11.2.17.3773.0.2 .1.3.6.1.4.1.11150.3.4.0.1 .1.3.6.1.4.1.11150.3.4.0.2 .1.3.6.1.4.1.11150.3.4.0.11 .1.3.6.1.4.1.11150.3.4.0.21