Sort a list of object identifiers: Difference between revisions

Content added Content deleted
(Added solution for Action!)
m (syntax highlighting fixup automation)
Line 47: Line 47:
{{trans|Python}}
{{trans|Python}}


<lang 11l>V data = [
<syntaxhighlight lang="11l">V data = [
‘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.10’,
‘1.3.6.1.4.1.11.2.17.5.2.0.79’,
‘1.3.6.1.4.1.11.2.17.5.2.0.79’,
Line 59: Line 59:


L(s) sorted(data, key' x -> x.split(:delim).map(Int))
L(s) sorted(data, key' x -> x.split(:delim).map(Int))
print(s)</lang>
print(s)</syntaxhighlight>


{{out}}
{{out}}
Line 72: Line 72:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>DEFINE PTR="CARD"
<syntaxhighlight lang="action!">DEFINE PTR="CARD"


PROC PrintArray(PTR ARRAY a INT size)
PROC PrintArray(PTR ARRAY a INT size)
Line 168: Line 168:
PrintE("Array after sort:")
PrintE("Array after sort:")
PrintArray(a,SIZE)
PrintArray(a,SIZE)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_a_list_of_object_identifiers.png Screenshot from Atari 8-bit computer]
[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: Line 193:
{{works with|Ada|Ada|2012}}
{{works with|Ada|Ada|2012}}


<lang Ada>with Ada.Containers.Generic_Array_Sort;
<syntaxhighlight lang="ada">with Ada.Containers.Generic_Array_Sort;
with Ada.Strings.Fixed;
with Ada.Strings.Fixed;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 243: Line 243:
Ada.Text_IO.Put_Line(To_String(element));
Ada.Text_IO.Put_Line(To_String(element));
end loop;
end loop;
end Sort_List_Identifiers;</lang>
end Sort_List_Identifiers;</syntaxhighlight>
{{out}}
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 256: Line 256:
===Vanilla===
===Vanilla===
Place the call to the sort handler in a <tt>considering numeric strings</tt> statement.
Place the call to the sort handler in a <tt>considering numeric strings</tt> statement.
<lang applescript>(* Shell sort
<syntaxhighlight lang="applescript">(* Shell sort
Algorithm: Donald Shell, 1959.
Algorithm: Donald Shell, 1959.
*)
*)
Line 302: Line 302:
sort(theList, 1, -1)
sort(theList, 1, -1)
end considering
end considering
return theList</lang>
return theList</syntaxhighlight>


{{output}}
{{output}}
Line 309: Line 309:
===ASObjC===
===ASObjC===
Use the <tt>localizedStandardCompare:</tt> string comparison method.
Use the <tt>localizedStandardCompare:</tt> string comparison method.
<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use framework "Foundation"


Line 319: Line 319:
ascending:(true) selector:("localizedStandardCompare:")
ascending:(true) selector:("localizedStandardCompare:")
tell theArray to sortUsingDescriptors:({theDescriptor})
tell theArray to sortUsingDescriptors:({theDescriptor})
return theArray as list</lang>
return theArray as list</syntaxhighlight>


{{output}}
{{output}}
Line 328: Line 328:
As a composition of pure functions:
As a composition of pure functions:


<lang applescript>------------- SORTED LIST OF OBJECT IDENTIFIERS ------------
<syntaxhighlight lang="applescript">------------- SORTED LIST OF OBJECT IDENTIFIERS ------------


-- sortedIdentifiers :: [String] -> [String]
-- sortedIdentifiers :: [String] -> [String]
Line 686: Line 686:
end tell
end tell
end if
end if
end zipWith</lang>
end zipWith</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 696: Line 696:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>; based on http://www.rosettacode.org/wiki/Sorting_algorithms/Quicksort#AutoHotkey
<syntaxhighlight lang="autohotkey">; based on http://www.rosettacode.org/wiki/Sorting_algorithms/Quicksort#AutoHotkey
OidQuickSort(a, Delim:=".", index:=1){
OidQuickSort(a, Delim:=".", index:=1){
if (a.Count() <= 1)
if (a.Count() <= 1)
Line 720: Line 720:
Out.InsertAt(1, Less*) ; InsertAt all values of Less at index 1
Out.InsertAt(1, Less*) ; InsertAt all values of Less at index 1
return Out
return Out
}</lang>
}</syntaxhighlight>
Examples:<lang AutoHotkey>a := ["1.3.6.1.4.1.11.2.17.19.3.4.0.10"
Examples:<syntaxhighlight lang="autohotkey">a := ["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.5.2.0.79"
,"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.4"
Line 731: Line 731:
Out .= "`n" v
Out .= "`n" v
MsgBox % Out
MsgBox % Out
return</lang>
return</syntaxhighlight>
{{out}}
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 742: Line 742:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SORT_A_LIST_OF_OBJECT_IDENTIFIERS.AWK
# syntax: GAWK -f SORT_A_LIST_OF_OBJECT_IDENTIFIERS.AWK
#
#
Line 778: Line 778:
exit(0)
exit(0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 791: Line 791:
=={{header|C}}==
=={{header|C}}==
A C99 (or later) compiler is required.
A C99 (or later) compiler is required.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 900: Line 900:
oid_destroy(oids[i]);
oid_destroy(oids[i]);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 913: Line 913:


=={{header|C sharp}}==
=={{header|C sharp}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Generic;
Line 940: Line 940:
Console.WriteLine(string.Join(Environment.NewLine, oids));
Console.WriteLine(string.Join(Environment.NewLine, oids));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 952: Line 952:


=={{header|C++}}==
=={{header|C++}}==
<lang Cpp>#include <string>
<syntaxhighlight lang="cpp">#include <string>
#include <vector>
#include <vector>
#include <algorithm>
#include <algorithm>
Line 1,001: Line 1,001:
std::cout << s << '\n' ;
std::cout << s << '\n' ;
return 0 ;
return 0 ;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,015: Line 1,015:
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.
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">
<lang Clojure>
(defn oid-vec [oid-str]
(defn oid-vec [oid-str]
(->> (clojure.string/split oid-str #"\.")
(->> (clojure.string/split oid-str #"\.")
Line 1,040: Line 1,040:
(sort oid-compare)
(sort oid-compare)
(map oid-str)))
(map oid-str)))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,060: Line 1,060:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defun oid->list (oid)
<syntaxhighlight lang="lisp">(defun oid->list (oid)
(loop for start = 0 then (1+ pos)
(loop for start = 0 then (1+ pos)
for pos = (position #\. oid :start start)
for pos = (position #\. oid :start start)
Line 1,086: Line 1,086:
"1.3.6.1.4.1.11150.3.4.0")))
"1.3.6.1.4.1.11150.3.4.0")))
(dolist (oid (sort-oids oids))
(dolist (oid (sort-oids oids))
(write-line oid))))</lang>
(write-line oid))))</syntaxhighlight>
{{out}}
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,096: Line 1,096:


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule Sort_by_OID do
<syntaxhighlight lang="elixir">defmodule Sort_by_OID do
def numbers(list) do
def numbers(list) do
Enum.sort_by(list, fn oid ->
Enum.sort_by(list, fn oid ->
Line 1,113: Line 1,113:
]
]
|> Sort_by_OID.numbers
|> Sort_by_OID.numbers
|> Enum.each(fn oid -> IO.puts oid end)</lang>
|> Enum.each(fn oid -> IO.puts oid end)</syntaxhighlight>


{{out}}
{{out}}
Line 1,127: Line 1,127:
=={{header|Factor}}==
=={{header|Factor}}==
Factor provides the <code>human<=></code> word which converts numbers in a string to integers before comparing them.
Factor provides the <code>human<=></code> word which converts numbers in a string to integers before comparing them.
<lang factor>USING: io qw sequences sorting sorting.human ;
<syntaxhighlight lang="factor">USING: io qw sequences sorting sorting.human ;


qw{
qw{
Line 1,136: Line 1,136:
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.1
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0
} [ human<=> ] sort [ print ] each</lang>
} [ human<=> ] sort [ print ] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,156: Line 1,156:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,227: Line 1,227:
fmt.Println(o)
fmt.Println(o)
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,240: Line 1,240:
=={{header|Haskell}}==
=={{header|Haskell}}==
====Data.List====
====Data.List====
<lang Haskell>import Data.List ( sort , intercalate )
<syntaxhighlight lang="haskell">import Data.List ( sort , intercalate )


splitString :: Eq a => (a) -> [a] -> [[a]]
splitString :: Eq a => (a) -> [a] -> [[a]]
Line 1,264: Line 1,264:
main :: IO ( )
main :: IO ( )
main = do
main = do
mapM_ putStrLn $ orderOID oid</lang>
mapM_ putStrLn $ orderOID oid</syntaxhighlight>


{{out}}
{{out}}
Line 1,278: Line 1,278:
(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)
(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)


<lang haskell>import Data.Text (pack, split, unpack)
<syntaxhighlight lang="haskell">import Data.Text (pack, split, unpack)
import Data.List (sort, intercalate)
import Data.List (sort, intercalate)


Line 1,305: Line 1,305:
, "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.1"
, "1.3.6.1.4.1.11150.3.4.0"
, "1.3.6.1.4.1.11150.3.4.0"
]</lang>
]</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,317: Line 1,317:
we can alternatively write:
we can alternatively write:


<lang haskell>import Data.List.Split (splitOn)
<syntaxhighlight lang="haskell">import Data.List.Split (splitOn)
import Data.List (sort, intercalate)
import Data.List (sort, intercalate)


Line 1,326: Line 1,326:


readInt :: String -> Int
readInt :: String -> Int
readInt x = read x :: Int</lang>
readInt x = read x :: Int</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
Line 1,332: Line 1,332:
Data:
Data:


<lang J>oids=:<@-.&' ';._2]0 :0
<syntaxhighlight 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.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.5.2.0.79
Line 1,339: Line 1,339:
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.1
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0
)</lang>
)</syntaxhighlight>


In other words, for each line in that script, remove the spaces and put the rest in a box.
In other words, for each line in that script, remove the spaces and put the rest in a box.
Line 1,345: Line 1,345:
Sorting:
Sorting:


<lang J> >(/: __&".;._1&.('.'&,)&>) oids
<syntaxhighlight lang="j"> >(/: __&".;._1&.('.'&,)&>) oids
1.3.6.1.4.1.11.2.17.5.2.0.79
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.1
Line 1,351: Line 1,351:
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.10
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0.1 </lang>
1.3.6.1.4.1.11150.3.4.0.1 </syntaxhighlight>


In other words, for our sort key, we break the contents of each box by an initial '.' and treat the remainder as numbers.
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: Line 1,360:
{{works with|Java|8 or higher}}
{{works with|Java|8 or higher}}


<lang java>
<syntaxhighlight lang="java">
package com.rosettacode;
package com.rosettacode;


Line 1,393: Line 1,393:
.forEach(System.out::println);
.forEach(System.out::println);
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,404: Line 1,404:


=={{header|jq}}==
=={{header|jq}}==
<lang jq>def data: [
<syntaxhighlight lang="jq">def data: [
"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.10",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
Line 1,413: Line 1,413:
];
];


data | map( split(".") | map(tonumber) ) | sort | map(join("."))</lang>
data | map( split(".") | map(tonumber) ) | sort | map(join("."))</syntaxhighlight>


{{out}}
{{out}}
Line 1,428: Line 1,428:
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}


<lang julia>oidlist = ["1.3.6.1.4.1.11.2.17.19.3.4.0.10",
<syntaxhighlight lang="julia">oidlist = ["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.5.2.0.79",
"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.4",
Line 1,437: Line 1,437:
sort!(oidlist; lt=lexless,
sort!(oidlist; lt=lexless,
by=x -> parse.(Int, String.(split(x, "."))))
by=x -> parse.(Int, String.(split(x, "."))))
println.(oidlist)</lang>
println.(oidlist)</syntaxhighlight>


{{out}}
{{out}}
Line 1,448: Line 1,448:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


class Oid(val id: String): Comparable<Oid> {
class Oid(val id: String): Comparable<Oid> {
Line 1,475: Line 1,475:
)
)
println(oids.sorted().joinToString("\n"))
println(oids.sorted().joinToString("\n"))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,489: Line 1,489:
=={{header|Lua}}==
=={{header|Lua}}==
Using the in-built table.sort with a custom compare function.
Using the in-built table.sort with a custom compare function.
<lang Lua>local OIDs = {
<syntaxhighlight lang="lua">local OIDs = {
"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.10",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
Line 1,509: Line 1,509:
table.sort(OIDs, compare)
table.sort(OIDs, compare)
for _, oid in pairs(OIDs) do print(oid) end</lang>
for _, oid in pairs(OIDs) do print(oid) end</syntaxhighlight>
{{out}}
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,518: Line 1,518:
1.3.6.1.4.1.11150.3.4.0.1</pre>
1.3.6.1.4.1.11150.3.4.0.1</pre>
===Using Coroutine===
===Using Coroutine===
<lang lua>
<syntaxhighlight lang="lua">
local function oidGen(s)
local function oidGen(s)
local wrap, yield = coroutine.wrap, coroutine.yield
local wrap, yield = coroutine.wrap, coroutine.yield
Line 1,547: Line 1,547:
table.sort(OIDs, oidCmp)
table.sort(OIDs, oidCmp)
for _, oid in pairs(OIDs) do print(oid) end
for _, oid in pairs(OIDs) do print(oid) end
</syntaxhighlight>
</lang>


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
In this example we have to change dot to #, to make each number as an integer one.
In this example we have to change dot to #, to make each number as an integer one.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Module CheckIt {
Flush ' empty stack of values
Flush ' empty stack of values
Line 1,574: Line 1,574:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,594: Line 1,594:




<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
GT=lambda (a$, b$)->{
GT=lambda (a$, b$)->{
def i
def i
Line 1,628: Line 1,628:
}
}
}
}
</syntaxhighlight>
</lang>


Using a function which split pieces one time. We have to insert one more item, by append a "." to a$ and b$
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">
<lang M2000 Interpreter>
GT=lambda (a$, b$)->{
GT=lambda (a$, b$)->{
def i=-1
def i=-1
Line 1,644: Line 1,644:
=val(a$(i))>val(b$(i))
=val(a$(i))>val(b$(i))
}
}
</syntaxhighlight>
</lang>


===Using QuickSort===
===Using QuickSort===
Line 1,657: Line 1,657:




<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Group Quick {
Group Quick {
Private:
Private:
Line 1,720: Line 1,720:
Print join$(arr(i))
Print join$(arr(i))
}
}
</syntaxhighlight>
</lang>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>in = {"1.3.6.1.4.1.11.2.17.19.3.4.0.10",
<syntaxhighlight lang="mathematica">in = {"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.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.1", "1.3.6.1.4.1.11.2.17.19.3.4.0.1",
Line 1,729: Line 1,729:
in = StringSplit[#, "."] & /@ in;
in = StringSplit[#, "."] & /@ in;
in = Map[ToExpression, in, {2}];
in = Map[ToExpression, in, {2}];
Column[StringRiffle[ToString /@ #, "."] & /@ LexicographicSort[in]]</lang>
Column[StringRiffle[ToString /@ #, "."] & /@ LexicographicSort[in]]</syntaxhighlight>
{{out}}
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,741: Line 1,741:
=== OID as distinct string ===
=== 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:
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:
<lang Nim>import algorithm, sequtils, strutils
<syntaxhighlight lang="nim">import algorithm, sequtils, strutils


type OID = distinct string
type OID = distinct string
Line 1,775: Line 1,775:


for oid in OIDS.sorted(oidCmp):
for oid in OIDS.sorted(oidCmp):
echo oid</lang>
echo oid</syntaxhighlight>


Note that as the type is distinct, we have to borrow the procedure `$` to the string type in order to print OID values.
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: Line 1,793:
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”.
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”.


<lang Nim>import algorithm, sequtils, strutils
<syntaxhighlight lang="nim">import algorithm, sequtils, strutils


type OID = object
type OID = object
Line 1,824: Line 1,824:


for oid in OIDS.sorted(oidCmp):
for oid in OIDS.sorted(oidCmp):
echo oid</lang>
echo oid</syntaxhighlight>


{{out}}
{{out}}
Line 1,831: Line 1,831:
=={{header|Perl}}==
=={{header|Perl}}==


<lang perl>my @OIDs = qw(
<syntaxhighlight lang="perl">my @OIDs = qw(
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.10
1.3.6.1.4.1.11.2.17.5.2.0.79
1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,846: Line 1,846:
@OIDs;
@OIDs;


print "$_\n" for @sorted;</lang>
print "$_\n" for @sorted;</syntaxhighlight>


{{out}}
{{out}}
Line 1,860: Line 1,860:
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.
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.


<lang perl>my @sorted =
<syntaxhighlight lang="perl">my @sorted =
map { $_->[0] }
map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
sort { $a->[1] cmp $b->[1] }
map { [$_, eval "v$_"] }
map { [$_, eval "v$_"] }
@OIDs;</lang>
@OIDs;</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
{{libheader|Phix/basics}}
{{libheader|Phix/basics}}
This is a variation on a standard tagsort, but performed a bit more explicitly.
This is a variation on a standard tagsort, but performed a bit more explicitly.
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<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: #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>
<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: Line 1,891:
<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: #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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,904: Line 1,904:
===alternative===
===alternative===
This is very similar to the above, but without using any tags/indexes at all.
This is very similar to the above, but without using any tags/indexes at all.
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<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: #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>
<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: Line 1,918:
<span style="color: #000080;font-style:italic;">-- sort on sortable, then use vslice to extract the originals:</span>
<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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,930: Line 1,930:


=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
<lang Phixmonti>include ..\Utilitys.pmt
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt


( "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.10"
Line 1,963: Line 1,963:


len for get print nl endfor
len for get print nl endfor
</syntaxhighlight>
</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(for I
<syntaxhighlight lang="picolisp">(for I
(by
(by
'((L) (mapcar format (split (chop L) ".")))
'((L) (mapcar format (split (chop L) ".")))
Line 1,977: Line 1,977:
"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.1"
"1.3.6.1.4.1.11150.3.4.0" ) )
"1.3.6.1.4.1.11150.3.4.0" ) )
(prinl I) )</lang>
(prinl I) )</syntaxhighlight>
{{out}}
{{out}}
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
<pre>1.3.6.1.4.1.11.2.17.5.2.0.79
Line 1,988: Line 1,988:
=={{header|Prolog}}==
=={{header|Prolog}}==
{{works with|SWI Prolog}}
{{works with|SWI Prolog}}
<lang prolog>main:-
<syntaxhighlight lang="prolog">main:-
sort_oid_list(["1.3.6.1.4.1.11.2.17.19.3.4.0.10",
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",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
Line 2,013: Line 2,013:
number_strings([Number|Numbers], [String|Strings]):-
number_strings([Number|Numbers], [String|Strings]):-
number_string(Number, String),
number_string(Number, String),
number_strings(Numbers, Strings).</lang>
number_strings(Numbers, Strings).</syntaxhighlight>


{{out}}
{{out}}
Line 2,028: Line 2,028:


We need to split the input and map each part to int otherwise elements gets compared as a string
We need to split the input and map each part to int otherwise elements gets compared as a string
<syntaxhighlight lang="python">
<lang Python>
data = [
data = [
'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.10',
Line 2,040: Line 2,040:
for s in sorted(data, key=lambda x: list(map(int, x.split('.')))):
for s in sorted(data, key=lambda x: list(map(int, x.split('.')))):
print(s)
print(s)
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require data/order)
(require data/order)


Line 2,070: Line 2,070:
"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.10"
"1.3.6.1.4.1.11150.3.4.0"
"1.3.6.1.4.1.11150.3.4.0"
"1.3.6.1.4.1.11150.3.4.0.1")))</lang>
"1.3.6.1.4.1.11150.3.4.0.1")))</syntaxhighlight>
Tests run with no output, indicating success.
Tests run with no output, indicating success.


Line 2,078: Line 2,078:
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.
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.


<lang perl6>.say for sort *.comb(/\d+/)».Int, <
<syntaxhighlight lang="raku" line>.say for sort *.comb(/\d+/)».Int, <
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.10
1.3.6.1.4.1.11.2.17.5.2.0.79
1.3.6.1.4.1.11.2.17.5.2.0.79
Line 2,085: Line 2,085:
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.1
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0
>;</lang>
>;</syntaxhighlight>


{{out}}
{{out}}
Line 2,099: Line 2,099:
Alternatively, using the <tt>sprintf</tt>-based approach used by the Perl solution, for comparison ''(input elided)'':
Alternatively, using the <tt>sprintf</tt>-based approach used by the Perl solution, for comparison ''(input elided)'':


<lang perl6>.say for sort *.split('.').fmt('%08d'), <...>;</lang>
<syntaxhighlight lang="raku" line>.say for sort *.split('.').fmt('%08d'), <...>;</syntaxhighlight>


Or if using a third-party module is acceptable:
Or if using a third-party module is acceptable:


<lang perl6>use Sort::Naturally;
<syntaxhighlight lang="raku" line>use Sort::Naturally;


.say for sort &naturally, <...>;</lang>
.say for sort &naturally, <...>;</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
This REXX version supports negative integers in the OID.
This REXX version supports negative integers in the OID.
<lang rexx>/*REXX program performs a sort of OID (Object IDentifiers ◄── used in Network data).*/
<syntaxhighlight lang="rexx">/*REXX program performs a sort of OID (Object IDentifiers ◄── used in Network data).*/
call gen /*generate an array (@.) from the OIDs.*/
call gen /*generate an array (@.) from the OIDs.*/
call show 'before sort ───► ' /*display the @ array before sorting.*/
call show 'before sort ───► ' /*display the @ array before sorting.*/
Line 2,147: Line 2,147:
return /*── ─ ─ */
return /*── ─ ─ */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do a=1 for #; say right("OID number",20) right(a,length(#)) arg(1) @.a; end; return</lang>
show: do a=1 for #; say right("OID number",20) right(a,length(#)) arg(1) @.a; end; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the (internal) default input:}}
{{out|output|text=&nbsp; when using the (internal) default input:}}
<pre>
<pre>
Line 2,167: Line 2,167:
=={{header|Ring}}==
=={{header|Ring}}==


<syntaxhighlight lang="ring">
<lang Ring>


/*
/*
Line 2,276: Line 2,276:
###-----------------------------------------------------------
###-----------------------------------------------------------


>;</lang>
>;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,291: Line 2,291:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>%w[
<syntaxhighlight lang="ruby">%w[
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.10
1.3.6.1.4.1.11.2.17.5.2.0.79
1.3.6.1.4.1.11.2.17.5.2.0.79
Line 2,300: Line 2,300:
]
]
.sort_by{|oid| oid.split(".").map(&:to_i)}
.sort_by{|oid| oid.split(".").map(&:to_i)}
.each{|oid| puts oid}</lang>
.each{|oid| puts oid}</syntaxhighlight>


{{out}}
{{out}}
Line 2,312: Line 2,312:
</pre>
</pre>
Or, using the Gem module (which knows about versions):
Or, using the Gem module (which knows about versions):
<lang ruby>puts %w[
<syntaxhighlight lang="ruby">puts %w[
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.10
1.3.6.1.4.1.11.2.17.5.2.0.79
1.3.6.1.4.1.11.2.17.5.2.0.79
Line 2,319: Line 2,319:
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.1
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0
].sort_by{|oid| Gem::Version.new(oid) }</lang>
].sort_by{|oid| Gem::Version.new(oid) }</syntaxhighlight>
with identical output.
with identical output.


=={{header|Rust}}==
=={{header|Rust}}==
<lang Rust>fn split(s: &str) -> impl Iterator<Item = u64> + '_ {
<syntaxhighlight lang="rust">fn split(s: &str) -> impl Iterator<Item = u64> + '_ {
s.split('.').map(|x| x.parse().unwrap())
s.split('.').map(|x| x.parse().unwrap())
}
}
Line 2,340: Line 2,340:
println!("{:#?}", oids);
println!("{:#?}", oids);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[
<pre>[
Line 2,352: Line 2,352:


=={{header|Sather}}==
=={{header|Sather}}==
<lang sather>class MAIN is
<syntaxhighlight lang="sather">class MAIN is
oid_lt (a, b: STR): BOOL is
oid_lt (a, b: STR): BOOL is
as ::= a.cursor.split('.');
as ::= a.cursor.split('.');
Line 2,381: Line 2,381:
loop #OUT+sorted.elt! + "\n"; end;
loop #OUT+sorted.elt! + "\n"; end;
end;
end;
end;</lang>
end;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,401: Line 2,401:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func sort_OIDs(ids) {
<syntaxhighlight lang="ruby">func sort_OIDs(ids) {
ids.sort_by { |id|
ids.sort_by { |id|
id.split('.').map { Num(_) }
id.split('.').map { Num(_) }
Line 2,416: Line 2,416:
)
)


sort_OIDs(OIDs).each { .say }</lang>
sort_OIDs(OIDs).each { .say }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,429: Line 2,429:
=={{header|Swift}}==
=={{header|Swift}}==


<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


public struct OID {
public struct OID {
Line 2,478: Line 2,478:
for id in ids.sorted() {
for id in ids.sorted() {
print(id)
print(id)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,490: Line 2,490:


=={{header|Tcl}}==
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">
<lang Tcl>
# Example input data:
# Example input data:
set oid_list [list \
set oid_list [list \
Line 2,543: Line 2,543:
puts [join $oid_list "."]
puts [join $oid_list "."]
}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,557: Line 2,557:


=={{header|VBScript}}==
=={{header|VBScript}}==
<lang vb>' Sort a list of object identifiers - VBScript
<syntaxhighlight lang="vb">' Sort a list of object identifiers - VBScript
function myCompare(x,y)
function myCompare(x,y)
dim i,b
dim i,b
Line 2,597: Line 2,597:
"1.3.6.1.4.1.11150.3.4.0")
"1.3.6.1.4.1.11150.3.4.0")
bubbleSort a
bubbleSort a
wscript.echo join(a,vbCrlf) </lang>
wscript.echo join(a,vbCrlf) </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,611: Line 2,611:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/sort" for Sort
import "/sort" for Sort


Line 2,626: Line 2,626:
Sort.quick(oids)
Sort.quick(oids)
oids = oids.map { |oid| oid.replace(" ", "") }.toList
oids = oids.map { |oid| oid.replace(" ", "") }.toList
System.print(oids.join("\n"))</lang>
System.print(oids.join("\n"))</syntaxhighlight>


{{out}}
{{out}}
Line 2,642: Line 2,642:


Basically, blow apart each line into a list of numbers and sort that.
Basically, blow apart each line into a list of numbers and sort that.
<lang zkl>fcn sortOIDS(oids){ // oids is not modified, a new list is created
<syntaxhighlight lang="zkl">fcn sortOIDS(oids){ // oids is not modified, a new list is created
// pad each oid with a terminal (-1) so zip won't short cut
// pad each oid with a terminal (-1) so zip won't short cut
oids=oids.pump(List(),fcn(oid){ (oid + ".-1").split(".").apply("toInt") });
oids=oids.pump(List(),fcn(oid){ (oid + ".-1").split(".").apply("toInt") });
Line 2,653: Line 2,653:
});
});
oids.pump(List,fcn(list){ list[0,-1].concat(".") }) // back to strings
oids.pump(List,fcn(list){ list[0,-1].concat(".") }) // back to strings
}</lang>
}</syntaxhighlight>
<lang zkl>oids:=List(
<syntaxhighlight lang="zkl">oids:=List(
"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.10",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
"1.3.6.1.4.1.11.2.17.5.2.0.79",
Line 2,662: Line 2,662:
"1.3.6.1.4.1.11150.3.4.0");
"1.3.6.1.4.1.11150.3.4.0");
oids=sortOIDS(oids);
oids=sortOIDS(oids);
oids.pump(Console.println); // print one OID per line</lang>
oids.pump(Console.println); // print one OID per line</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>