Sort a list of object identifiers: Difference between revisions
Sort a list of object identifiers (view source)
Revision as of 10:44, 8 February 2024
, 4 months ago→{{header|Wren}}: Minor tidy
(Added solution for Action!) |
m (→{{header|Wren}}: Minor tidy) |
||
(7 intermediate revisions by 4 users not shown) | |||
Line 47:
{{trans|Python}}
<
‘1.3.6.1.4.1.11.2.17.19.3.4.0.10’,
‘1.3.6.1.4.1.11.2.17.5.2.0.79’,
Line 59:
L(s) sorted(data, key' x -> x.split(:delim).map(Int))
print(s)</
{{out}}
Line 72:
=={{header|Action!}}==
<
PROC PrintArray(PTR ARRAY a INT size)
Line 168:
PrintE("Array after sort:")
PrintArray(a,SIZE)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_a_list_of_object_identifiers.png Screenshot from Atari 8-bit computer]
Line 193:
{{works with|Ada|Ada|2012}}
<
with Ada.Strings.Fixed;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 243:
Ada.Text_IO.Put_Line(To_String(element));
end loop;
end Sort_List_Identifiers;</
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 256:
===Vanilla===
Place the call to the sort handler in a <tt>considering numeric strings</tt> statement.
<
Algorithm: Donald Shell, 1959.
*)
Line 302:
sort(theList, 1, -1)
end considering
return theList</
{{output}}
Line 309:
===ASObjC===
Use the <tt>localizedStandardCompare:</tt> string comparison method.
<
use framework "Foundation"
Line 319:
ascending:(true) selector:("localizedStandardCompare:")
tell theArray to sortUsingDescriptors:({theDescriptor})
return theArray as list</
{{output}}
Line 328:
As a composition of pure functions:
<
-- sortedIdentifiers :: [String] -> [String]
Line 686:
end tell
end if
end zipWith</
{{Out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 694:
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0.1</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">lst: [
"1.3.6.1.4.1.11.2.17.19.3.4.0.10"
"1.3.6.1.4.1.11.2.17.5.2.0.79"
"1.3.6.1.4.1.11.2.17.19.3.4.0.4"
"1.3.6.1.4.1.11150.3.4.0.1"
"1.3.6.1.4.1.11.2.17.19.3.4.0.1"
"1.3.6.1.4.1.11150.3.4.0"
]
print.lines arrange lst 'x ->
sum map.with:'i to [:integer] split.by:"." x 'z ->
z * 10^(20-i)</syntaxhighlight>
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.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.4
1.3.6.1.4.1.11.2.17.19.3.4.0.10
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0.1</pre>
=={{header|ATS}}==
<syntaxhighlight lang="ats">
(* Sort a list of object identifiers (OID). *)
#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"
%{^
#include <alloca.h> /* ATS2 exceptions require alloca(3). */
%}
#define NIL list_nil ()
#define :: list_cons
infixl ( :: ) :::
#define VNIL list_vt_nil ()
#define ::: list_vt_cons
exception Exc_not_an_oid of (string, string)
exception Exc_internal_error of (string)
macdef not_an_oid (s) = $raise Exc_not_an_oid ($mylocation, ,(s))
macdef internal_error () = $raise Exc_internal_error ($mylocation)
fn
oid_explode
{n : int}
(s : string n)
:<!exnwrt> List0_vt String1 =
let
fun
loop {i : nat | i <= n}
.<n - i>.
(lst : &list_vt (String1, 0) >> List0_vt String1,
i : size_t i)
:<!exnwrt> void =
if ~string_is_atend (s, i) then
let
fun
scan_number
{j : int | i <= j; j <= n}
.<n - j>.
(j : size_t j)
:<> [j : int | i <= j; j <= n]
size_t j =
if string_is_atend (s, j) then
j
else if ~isdigit s[j] then
j
else
scan_number (succ j)
val j = scan_number i
in
if j = i then
not_an_oid s
else if string_is_atend (s, j) then
let
val t =
strnptr2string (string_make_substring (s, i, j - i))
val+ ~ VNIL = lst
in
lst := t ::: VNIL
end
else if s[j] <> '.' then
not_an_oid s
else if string_is_atend (s, succ j) then
not_an_oid s
else
let
val+ ~ VNIL = lst
val t =
strnptr2string
(string_make_substring (s, i, j - i))
val () = lst := t ::: VNIL
val+ @ _ ::: tail = lst
val () = loop (tail, succ j)
prval () = fold@ lst
in
end
end
prval () = lemma_string_param s
var lst : List0_vt String1 = VNIL
in
if string_is_empty s then
not_an_oid s;
loop (lst, i2sz 0);
lst
end
fn
oid_field_cmp
(x : String0,
y : String0)
:<!exn> int =
let
fun
to_number {n : nat}
{i : nat | i <= n}
.<n - i>.
(s : string n,
i : size_t i,
accum : ullint)
:<!exn> ullint =
if string_is_atend (s, i) then
accum
else if ~isdigit s[i] then
internal_error ()
else
let
val digit = $UN.cast{ullint} s[i] - $UN.cast '0'
in
to_number (s, succ i, (10ULL * accum) + digit)
end
val nx = to_number (x, i2sz 0, 0ULL)
and ny = to_number (y, i2sz 0, 0ULL)
in
compare (nx, ny)
end
fn
oid_cmp (x : String0,
y : String0)
:<!exnwrt> int =
let
fun
loop {m, n : nat}
.<m, n>.
(xs : list (String1, m),
ys : list (String1, n))
:<!exnwrt> int =
case+ ys of
| NIL => if iseqz xs then 0 else 1
| yhead :: ytail =>
begin
case+ xs of
| NIL => ~1
| xhead :: xtail =>
let
val cmp = oid_field_cmp (xhead, yhead)
in
if cmp = 0 then
loop (xtail, ytail)
else
cmp
end
end
val xs = list_vt2t (oid_explode x)
and ys = list_vt2t (oid_explode y)
in
loop (xs, ys)
end
fn
oid_list_sort
{n : int}
(lst : list (String0, n))
:<!exnwrt> list_vt (String0, n) =
list_mergesort_fun<String0>
(lst, lam (x, y) => $effmask_all oid_cmp (x, y))
implement
main0 () =
try
let
val oid_list =
$list ("1.3.6.1.4.1.11.2.17.19.3.4.0.10",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
"1.3.6.1.4.1.11.2.17.19.3.4.0.4",
"1.3.6.1.4.1.11150.3.4.0.1",
"1.3.6.1.4.1.11.2.17.19.3.4.0.1",
"1.3.6.1.4.1.11150.3.4.0")
val oid_sorted = list_vt2t (oid_list_sort oid_list)
var p : List0 String0
in
for (p := oid_sorted; isneqz p; p := list_tail p)
println! (list_head p)
end
with
| ~ Exc_not_an_oid (loc, s) =>
begin
println! ("Not a UID: \"", s, "\" (exception raised at ", loc, ")");
exit 1
end
| ~ Exc_internal_error loc =>
begin
println! ("Internal error (exception raised at ", loc, ")");
exit 1
end
</syntaxhighlight>
{{out}}
<pre>
$ patscc -DATS_MEMALLOC_GCBDW oid_sort.dats -lgc && ./a.out
1.3.6.1.4.1.11.2.17.5.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.4
1.3.6.1.4.1.11.2.17.19.3.4.0.10
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0.1
</pre>
=={{header|AutoHotkey}}==
<
OidQuickSort(a, Delim:=".", index:=1){
if (a.Count() <= 1)
Line 720 ⟶ 950:
Out.InsertAt(1, Less*) ; InsertAt all values of Less at index 1
return Out
}</
Examples:<
,"1.3.6.1.4.1.11.2.17.5.2.0.79"
,"1.3.6.1.4.1.11.2.17.19.3.4.0.4"
Line 731 ⟶ 961:
Out .= "`n" v
MsgBox % Out
return</
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 742 ⟶ 972:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SORT_A_LIST_OF_OBJECT_IDENTIFIERS.AWK
#
Line 778 ⟶ 1,008:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 791 ⟶ 1,021:
=={{header|C}}==
A C99 (or later) compiler is required.
<
#include <stdlib.h>
#include <string.h>
Line 900 ⟶ 1,130:
oid_destroy(oids[i]);
return 0;
}</
{{out}}
Line 913 ⟶ 1,143:
=={{header|C sharp}}==
<
using System.Linq;
using System.Collections.Generic;
Line 940 ⟶ 1,170:
Console.WriteLine(string.Join(Environment.NewLine, oids));
}
}</
{{out}}
<pre>
Line 952 ⟶ 1,182:
=={{header|C++}}==
<
#include <vector>
#include <algorithm>
Line 1,001 ⟶ 1,231:
std::cout << s << '\n' ;
return 0 ;
}</
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,015 ⟶ 1,245:
Clojure 'sort' function allows specifying an optional comparator function. In this case, our custom comparator utilizes the ability of the clojure.core 'compare' function to compare vectors in an appropriate fashion.
<syntaxhighlight lang="clojure">
(defn oid-vec [oid-str]
(->> (clojure.string/split oid-str #"\.")
Line 1,040 ⟶ 1,270:
(sort oid-compare)
(map oid-str)))
</syntaxhighlight>
{{out}}
Line 1,060 ⟶ 1,290:
=={{header|Common Lisp}}==
<
(loop for start = 0 then (1+ pos)
for pos = (position #\. oid :start start)
Line 1,086 ⟶ 1,316:
"1.3.6.1.4.1.11150.3.4.0")))
(dolist (oid (sort-oids oids))
(write-line oid))))</
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,096 ⟶ 1,326:
=={{header|Elixir}}==
<
def numbers(list) do
Enum.sort_by(list, fn oid ->
Line 1,113 ⟶ 1,343:
]
|> Sort_by_OID.numbers
|> Enum.each(fn oid -> IO.puts oid end)</
{{out}}
Line 1,127 ⟶ 1,357:
=={{header|Factor}}==
Factor provides the <code>human<=></code> word which converts numbers in a string to integers before comparing them.
<
qw{
Line 1,136 ⟶ 1,366:
1.3.6.1.4.1.11.2.17.19.3.4.0.1
1.3.6.1.4.1.11150.3.4.0
} [ human<=> ] sort [ print ] each</
{{out}}
<pre>
Line 1,149 ⟶ 1,379:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sort_a_list_of_object_identifiers}}
'''Solution'''
[[File:Fōrmulæ - Sort a list of object identifiers 01.png]]
[[File:Fōrmulæ - Sort a list of object identifiers 02.png]]
'''Test case'''
[[File:Fōrmulæ - Sort a list of object identifiers 03.png]]
[[File:Fōrmulæ - Sort a list of object identifiers 04.png]]
=={{header|Go}}==
<
import (
Line 1,227 ⟶ 1,465:
fmt.Println(o)
}
}</
{{out}}
<pre>
Line 1,240 ⟶ 1,478:
=={{header|Haskell}}==
====Data.List====
<
splitString :: Eq a => (a) -> [a] -> [[a]]
Line 1,264 ⟶ 1,502:
main :: IO ( )
main = do
mapM_ putStrLn $ orderOID oid</
{{out}}
Line 1,278 ⟶ 1,516:
(To use '''split :: (Char -> Bool) -> Text -> [Text]''' in the standard libraries, we would have to temporarily convert the strings from [Char] to Text with pack and unpack)
<
import Data.List (sort, intercalate)
Line 1,305 ⟶ 1,543:
, "1.3.6.1.4.1.11.2.17.19.3.4.0.1"
, "1.3.6.1.4.1.11150.3.4.0"
]</
{{Out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,317 ⟶ 1,555:
we can alternatively write:
<
import Data.List (sort, intercalate)
Line 1,326 ⟶ 1,564:
readInt :: String -> Int
readInt x = read x :: Int</
=={{header|J}}==
Line 1,332 ⟶ 1,570:
Data:
<
1.3.6.1.4.1.11.2.17.19.3.4.0.10
1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,339 ⟶ 1,577:
1.3.6.1.4.1.11.2.17.19.3.4.0.1
1.3.6.1.4.1.11150.3.4.0
)</
In other words, for each line in that script, remove the spaces and put the rest in a box.
Line 1,345 ⟶ 1,583:
Sorting:
<
1.3.6.1.4.1.11.2.17.5.2.0.79
1.3.6.1.4.1.11.2.17.19.3.4.0.1
Line 1,351 ⟶ 1,589:
1.3.6.1.4.1.11.2.17.19.3.4.0.10
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0.1 </
In other words, for our sort key, we break the contents of each box by an initial '.' and treat the remainder as numbers.
Line 1,360 ⟶ 1,598:
{{works with|Java|8 or higher}}
<
package com.rosettacode;
Line 1,393 ⟶ 1,631:
.forEach(System.out::println);
}
}</
{{out}}
Line 1,404 ⟶ 1,642:
=={{header|jq}}==
<
"1.3.6.1.4.1.11.2.17.19.3.4.0.10",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
Line 1,413 ⟶ 1,651:
];
data | map( split(".") | map(tonumber) ) | sort | map(join("."))</
{{out}}
Line 1,428 ⟶ 1,666:
{{works with|Julia|0.6}}
<
"1.3.6.1.4.1.11.2.17.5.2.0.79",
"1.3.6.1.4.1.11.2.17.19.3.4.0.4",
Line 1,437 ⟶ 1,675:
sort!(oidlist; lt=lexless,
by=x -> parse.(Int, String.(split(x, "."))))
println.(oidlist)</
{{out}}
Line 1,448 ⟶ 1,686:
=={{header|Kotlin}}==
<
class Oid(val id: String): Comparable<Oid> {
Line 1,475 ⟶ 1,713:
)
println(oids.sorted().joinToString("\n"))
}</
{{out}}
Line 1,489 ⟶ 1,727:
=={{header|Lua}}==
Using the in-built table.sort with a custom compare function.
<
"1.3.6.1.4.1.11.2.17.19.3.4.0.10",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
Line 1,509 ⟶ 1,747:
table.sort(OIDs, compare)
for _, oid in pairs(OIDs) do print(oid) end</
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,518 ⟶ 1,756:
1.3.6.1.4.1.11150.3.4.0.1</pre>
===Using Coroutine===
<
local function oidGen(s)
local wrap, yield = coroutine.wrap, coroutine.yield
Line 1,547 ⟶ 1,785:
table.sort(OIDs, oidCmp)
for _, oid in pairs(OIDs) do print(oid) end
</syntaxhighlight>
=={{header|M2000 Interpreter}}==
In this example we have to change dot to #, to make each number as an integer one.
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Flush ' empty stack of values
Line 1,574 ⟶ 1,812:
}
Checkit
</syntaxhighlight>
{{out}}
<pre>
Line 1,594 ⟶ 1,832:
<syntaxhighlight lang="m2000 interpreter">
GT=lambda (a$, b$)->{
def i
Line 1,628 ⟶ 1,866:
}
}
</syntaxhighlight>
Using a function which split pieces one time. We have to insert one more item, by append a "." to a$ and b$
<syntaxhighlight lang="m2000 interpreter">
GT=lambda (a$, b$)->{
def i=-1
Line 1,644 ⟶ 1,882:
=val(a$(i))>val(b$(i))
}
</syntaxhighlight>
===Using QuickSort===
Line 1,657 ⟶ 1,895:
<syntaxhighlight lang="m2000 interpreter">
Group Quick {
Private:
Line 1,720 ⟶ 1,958:
Print join$(arr(i))
}
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
"1.3.6.1.4.1.11.2.17.5.2.0.79", "1.3.6.1.4.1.11.2.17.19.3.4.0.4",
"1.3.6.1.4.1.11150.3.4.0.1", "1.3.6.1.4.1.11.2.17.19.3.4.0.1",
Line 1,729 ⟶ 1,967:
in = StringSplit[#, "."] & /@ in;
in = Map[ToExpression, in, {2}];
Column[StringRiffle[ToString /@ #, "."] & /@ LexicographicSort[in]]</
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,741 ⟶ 1,979:
=== OID as distinct string ===
Nim allows to define distinct types. As OID are peculiar strings, defining them as distinct strings seems a good idea. We have to define a specific comparison procedure and that’s all. Here is the code:
<
type OID = distinct string
Line 1,775 ⟶ 2,013:
for oid in OIDS.sorted(oidCmp):
echo oid</
Note that as the type is distinct, we have to borrow the procedure `$` to the string type in order to print OID values.
Line 1,793 ⟶ 2,031:
To avoid this, we can define OID as a composite object type containing a string value and a list of integers to use for the comparisons. The code is not really more complicated, only arguably less elegant as we have to create the OIDs using the procedure “initOID”.
<
type OID = object
Line 1,824 ⟶ 2,062:
for oid in OIDS.sorted(oidCmp):
echo oid</
{{out}}
Line 1,831 ⟶ 2,069:
=={{header|Perl}}==
<
1.3.6.1.4.1.11.2.17.19.3.4.0.10
1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,846 ⟶ 2,084:
@OIDs;
print "$_\n" for @sorted;</
{{out}}
Line 1,860 ⟶ 2,098:
Alternately, you can sort them as "version strings", which is a Perl syntax allowing you to specify a character string in the source code with the characters' codes specified as a dot-delimited sequence of integers.
<
map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [$_, eval "v$_"] }
@OIDs;</
=={{header|Phix}}==
{{libheader|Phix/basics}}
This is a variation on a standard tagsort, but performed a bit more explicitly.
<!--<
<span style="color: #004080;">sequence</span> <span style="color: #000000;">strings</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"1.3.6.1.4.1.11.2.17.19.3.4.0.10"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"1.3.6.1.4.1.11.2.17.5.2.0.79"</span><span style="color: #0000FF;">,</span>
Line 1,891 ⟶ 2,129:
<span style="color: #0000FF;">?</span><span style="color: #000000;">strings</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sortable</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 1,904 ⟶ 2,142:
===alternative===
This is very similar to the above, but without using any tags/indexes at all.
<!--<
<span style="color: #008080;">constant</span> <span style="color: #000000;">strings</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"1.3.6.1.4.1.11.2.17.19.3.4.0.10"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"1.3.6.1.4.1.11.2.17.5.2.0.79"</span><span style="color: #0000FF;">,</span>
Line 1,918 ⟶ 2,156:
<span style="color: #000080;font-style:italic;">-- sort on sortable, then use vslice to extract the originals:</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">strings</span><span style="color: #0000FF;">,</span><span style="color: #000000;">each</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">))</span>
<!--</
{{out}}
<pre>
Line 1,930 ⟶ 2,168:
=={{header|Phixmonti}}==
<
( "1.3.6.1.4.1.11.2.17.19.3.4.0.10"
Line 1,963 ⟶ 2,201:
len for get print nl endfor
</syntaxhighlight>
=={{header|PicoLisp}}==
<
(by
'((L) (mapcar format (split (chop L) ".")))
Line 1,977 ⟶ 2,215:
"1.3.6.1.4.1.11.2.17.19.3.4.0.1"
"1.3.6.1.4.1.11150.3.4.0" ) )
(prinl I) )</
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,988 ⟶ 2,226:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
sort_oid_list(["1.3.6.1.4.1.11.2.17.19.3.4.0.10",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
Line 2,013 ⟶ 2,251:
number_strings([Number|Numbers], [String|Strings]):-
number_string(Number, String),
number_strings(Numbers, Strings).</
{{out}}
Line 2,028 ⟶ 2,266:
We need to split the input and map each part to int otherwise elements gets compared as a string
<syntaxhighlight lang="python">
data = [
'1.3.6.1.4.1.11.2.17.19.3.4.0.10',
Line 2,040 ⟶ 2,278:
for s in sorted(data, key=lambda x: list(map(int, x.split('.')))):
print(s)
</syntaxhighlight>
=={{header|Racket}}==
<
(require data/order)
Line 2,070 ⟶ 2,308:
"1.3.6.1.4.1.11.2.17.19.3.4.0.10"
"1.3.6.1.4.1.11150.3.4.0"
"1.3.6.1.4.1.11150.3.4.0.1")))</
Tests run with no output, indicating success.
Line 2,078 ⟶ 2,316:
The <tt>sort</tt> routine accepts a sort key callback as the first argument. Here we generate a list of integers as the sort key for each OID, which gets sorted lexicographically with numeric comparison by default.
<syntaxhighlight lang="raku"
1.3.6.1.4.1.11.2.17.19.3.4.0.10
1.3.6.1.4.1.11.2.17.5.2.0.79
Line 2,085 ⟶ 2,323:
1.3.6.1.4.1.11.2.17.19.3.4.0.1
1.3.6.1.4.1.11150.3.4.0
>;</
{{out}}
Line 2,099 ⟶ 2,337:
Alternatively, using the <tt>sprintf</tt>-based approach used by the Perl solution, for comparison ''(input elided)'':
<syntaxhighlight lang="raku"
Or if using a third-party module is acceptable:
<syntaxhighlight lang="raku"
.say for sort &naturally, <...>;</
=={{header|REXX}}==
This REXX version supports negative integers in the OID.
<
call gen /*generate an array (@.) from the OIDs.*/
call show 'before sort ───► ' /*display the @ array before sorting.*/
Line 2,147 ⟶ 2,385:
return /*── ─ ─ */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do a=1 for #; say right("OID number",20) right(a,length(#)) arg(1) @.a; end; return</
{{out|output|text= when using the (internal) default input:}}
<pre>
Line 2,167 ⟶ 2,405:
=={{header|Ring}}==
<syntaxhighlight lang="ring">
/*
Line 2,276 ⟶ 2,514:
###-----------------------------------------------------------
>;</
{{out}}
<pre>
Line 2,291 ⟶ 2,529:
=={{header|Ruby}}==
<
1.3.6.1.4.1.11.2.17.19.3.4.0.10
1.3.6.1.4.1.11.2.17.5.2.0.79
Line 2,300 ⟶ 2,538:
]
.sort_by{|oid| oid.split(".").map(&:to_i)}
.each{|oid| puts oid}</
{{out}}
Line 2,312 ⟶ 2,550:
</pre>
Or, using the Gem module (which knows about versions):
<
1.3.6.1.4.1.11.2.17.19.3.4.0.10
1.3.6.1.4.1.11.2.17.5.2.0.79
Line 2,319 ⟶ 2,557:
1.3.6.1.4.1.11.2.17.19.3.4.0.1
1.3.6.1.4.1.11150.3.4.0
].sort_by{|oid| Gem::Version.new(oid) }</
with identical output.
=={{header|Rust}}==
<
s.split('.').map(|x| x.parse().unwrap())
}
Line 2,340 ⟶ 2,578:
println!("{:#?}", oids);
}</
{{out}}
<pre>[
Line 2,352 ⟶ 2,590:
=={{header|Sather}}==
<
oid_lt (a, b: STR): BOOL is
as ::= a.cursor.split('.');
Line 2,381 ⟶ 2,619:
loop #OUT+sorted.elt! + "\n"; end;
end;
end;</
{{out}}
<pre>
Line 2,401 ⟶ 2,639:
=={{header|Sidef}}==
<
ids.sort_by { |id|
id.split('.').map { Num(_) }
Line 2,416 ⟶ 2,654:
)
sort_OIDs(OIDs).each { .say }</
{{out}}
<pre>
Line 2,429 ⟶ 2,667:
=={{header|Swift}}==
<
public struct OID {
Line 2,478 ⟶ 2,716:
for id in ids.sorted() {
print(id)
}</
{{out}}
Line 2,490 ⟶ 2,728:
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">
# Example input data:
set oid_list [list \
Line 2,543 ⟶ 2,781:
puts [join $oid_list "."]
}
</syntaxhighlight>
{{out}}
Line 2,557 ⟶ 2,795:
=={{header|VBScript}}==
<
function myCompare(x,y)
dim i,b
Line 2,597 ⟶ 2,835:
"1.3.6.1.4.1.11150.3.4.0")
bubbleSort a
wscript.echo join(a,vbCrlf) </
{{out}}
<pre>
Line 2,611 ⟶ 2,849:
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
<
import "./sort" for Sort
var oids = [
Line 2,626 ⟶ 2,864:
Sort.quick(oids)
oids = oids.map { |oid| oid.replace(" ", "") }.toList
System.print(oids.join("\n"))</
{{out}}
Line 2,642 ⟶ 2,880:
Basically, blow apart each line into a list of numbers and sort that.
<
// pad each oid with a terminal (-1) so zip won't short cut
oids=oids.pump(List(),fcn(oid){ (oid + ".-1").split(".").apply("toInt") });
Line 2,653 ⟶ 2,891:
});
oids.pump(List,fcn(list){ list[0,-1].concat(".") }) // back to strings
}</
<
"1.3.6.1.4.1.11.2.17.19.3.4.0.10",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
Line 2,662 ⟶ 2,900:
"1.3.6.1.4.1.11150.3.4.0");
oids=sortOIDS(oids);
oids.pump(Console.println); // print one OID per line</
{{out}}
<pre>
|