Code: Select all
# Mikrotik bubble sort implementation
# v1.1
:local A 5,4,3,2,1;
:local B;
:local n [ :len $A ];
:local swapped;
:put "Before unsorted $A";
do {
:set swapped false;
:for i from=1 to=($n - 1) do={
:if ([ :pick $A ($i - 1) ] > [ :pick $A $i ]) do={
:set B [ :pick $A ($i - 1) ];
:set A ([ :pick $A 0 ($i - 1) ], [ :pick $A $i ], $B, [ :pick $A ($i + 1) [ :len $A ] ]);
:set swapped true;
}
}
:set n ($n - 1);
} while=($swapped);
:put "After sorted $A";
Code: Select all
:local ipAddrListName "some_list";
:local arrIpAddrList;
:local arrCommentList;
:local tempIpValue;
:local tempComment;
:local swapped false;
:local arrIdAddrList [ :toarray [ /ip firewall address-list find where list=$ipAddrListName ] ];
:if ([ :len $arrIdAddrList ] > 0) do={
:foreach i in $arrIdAddrList do={
:set arrIpAddrList ($arrIpAddrList, [ :toip [ /ip firewall address-list get $i address ] ]);
:set arrCommentList ($arrCommentList, [ /ip firewall address-list get $i comment ]);
}
:local n [ :len $arrIpAddrList ];
do {
:set swapped false;
:for i from=1 to=($n - 1) do={
:if ([ :pick $arrIpAddrList ($i - 1) ] > [ :pick $arrIpAddrList $i ]) do={
:set tempIpValue [ :pick $arrIpAddrList ($i - 1) ];
:set tempComment [ :pick $arrCommentList ($i - 1) ];
:set arrIpAddrList ([ :pick $arrIpAddrList 0 ($i - 1) ], [ :pick $arrIpAddrList $i ], $tempIpValue, [ :pick $arrIpAddrList ($i + 1) [ :len $arrIpAddrList ] ]);
:set arrCommentList ([ :pick $arrCommentList 0 ($i - 1) ], [ :pick $arrCommentList $i ], $tempComment, [ :pick $arrCommentList ($i + 1) [ :len $arrIpAddrList ] ]);
:set swapped true;
}
}
:set n ($n - 1);
} while=($swapped);
:if ($swapped = true) do={
# Remove the old entries
:foreach i in $arrIdAddrList do={
/ip firewall address-list remove $i;
}
# Save the sorted entries
:for i from=0 to=([ :len $arrIpAddrList ] - 1) do={
/ip firewall address-list add list=$ipAddrListName \
address=[ :pick $arrIpAddrList $i] comment="$[ :pick $arrCommentList $i ]";
}
}
}