Sort numbers lexicographically: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 18: | Line 18: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V n = 13 |
||
print(sorted(Array(1..n), key' i -> String(i)))</ |
print(sorted(Array(1..n), key' i -> String(i)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 27: | Line 27: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC PrintArray(INT ARRAY a INT size) |
||
INT i |
INT i |
||
Line 86: | Line 86: | ||
Test(a,COUNT_A) |
Test(a,COUNT_A) |
||
Test(b,COUNT_B) |
Test(b,COUNT_B) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_numbers_lexicographically.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_numbers_lexicographically.png Screenshot from Atari 8-bit computer] |
||
Line 102: | Line 102: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">WITH Ada.Containers.Generic_Array_Sort, Ada.Text_IO; |
||
USE Ada.Text_IO; |
USE Ada.Text_IO; |
||
PROCEDURE Main IS |
PROCEDURE Main IS |
||
Line 121: | Line 121: | ||
Show (21); |
Show (21); |
||
END Main; |
END Main; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 143: | Line 143: | ||
For fast execution of the task as specified, this take on the [https://www.rosettacode.org/wiki/Sort_numbers_lexicographically#BBC_BASIC BBC BASIC] method below generates the integers in the required order: |
For fast execution of the task as specified, this take on the [https://www.rosettacode.org/wiki/Sort_numbers_lexicographically#BBC_BASIC BBC BASIC] method below generates the integers in the required order: |
||
< |
<syntaxhighlight lang="applescript">on oneToNLexicographically(n) |
||
script o |
script o |
||
property output : {} |
property output : {} |
||
Line 167: | Line 167: | ||
oneToNLexicographically(123) |
oneToNLexicographically(123) |
||
--> {1, 10, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 11, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 12, 120, 121, 122, 123, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99}</ |
--> {1, 10, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 11, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 12, 120, 121, 122, 123, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99}</syntaxhighlight> |
||
In the unlikely event of it ever being necessary to sort a ''given'' list of integers in this fashion, one possibility is to create another list containing text versions of the integers and to sort this while rearranging the integer versions in parallel. |
In the unlikely event of it ever being necessary to sort a ''given'' list of integers in this fashion, one possibility is to create another list containing text versions of the integers and to sort this while rearranging the integer versions in parallel. |
||
< |
<syntaxhighlight lang="applescript">use sorter : script "Custom Iterative Ternary Merge Sort" -- <https://macscripter.net/viewtopic.php?pid=194430#p194430> |
||
on sortLexicographically(integerList) -- Sorts integerList in place. |
on sortLexicographically(integerList) -- Sorts integerList in place. |
||
Line 188: | Line 188: | ||
sortLexicographically(someIntegers) |
sortLexicographically(someIntegers) |
||
return someIntegers |
return someIntegers |
||
--> {-1, -10, -2, -3, -4, -5, -6, 0, 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}</ |
--> {-1, -10, -2, -3, -4, -5, -6, 0, 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">arr: 1..13 |
||
print sort map arr => [to :string &]</ |
print sort map arr => [to :string &]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 200: | Line 200: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">n2lexicog(n){ |
||
Arr := [], list := "" |
Arr := [], list := "" |
||
loop % n |
loop % n |
||
Line 208: | Line 208: | ||
Arr.Push(v) |
Arr.Push(v) |
||
return Arr |
return Arr |
||
}</ |
}</syntaxhighlight> |
||
Examples:< |
Examples:<syntaxhighlight lang="autohotkey">n := 13 |
||
x := n2lexicog(n) |
x := n2lexicog(n) |
||
for k, v in x |
for k, v in x |
||
output .= v ", " |
output .= v ", " |
||
MsgBox % "[" Trim(output, ", ") "]" ; show output |
MsgBox % "[" Trim(output, ", ") "]" ; show output |
||
return</ |
return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]</pre> |
<pre>[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]</pre> |
||
Line 220: | Line 220: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
===Robust with checks=== |
===Robust with checks=== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SORT_NUMBERS_LEXICOGRAPHICALLY.AWK |
# syntax: GAWK -f SORT_NUMBERS_LEXICOGRAPHICALLY.AWK |
||
# |
# |
||
Line 265: | Line 265: | ||
return(str) |
return(str) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 279: | Line 279: | ||
===Alternative, using GAWK's builtin sort=== |
===Alternative, using GAWK's builtin sort=== |
||
This version explicitly casts integers as strings during list generation and uses the builtin sort available in GAWK on element values. |
This version explicitly casts integers as strings during list generation and uses the builtin sort available in GAWK on element values. |
||
< |
<syntaxhighlight lang="awk">BEGIN { |
||
n=13 |
n=13 |
||
for (i=1; i<=n; i++) |
for (i=1; i<=n; i++) |
||
Line 286: | Line 286: | ||
for (k in a) |
for (k in a) |
||
printf "%d ", a[k] |
printf "%d ", a[k] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 293: | Line 293: | ||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
Create a delimited string with numbers and use SORT$. |
Create a delimited string with numbers and use SORT$. |
||
< |
<syntaxhighlight lang="bacon">CONST n = 13 |
||
FOR x = 1 TO n |
FOR x = 1 TO n |
||
result$ = APPEND$(result$, 0, STR$(x)) |
result$ = APPEND$(result$, 0, STR$(x)) |
||
NEXT |
NEXT |
||
PRINT SORT$(result$)</ |
PRINT SORT$(result$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre> |
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre> |
||
Line 303: | Line 303: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> N%=13 |
||
PRINT "[" LEFT$(FNLexOrder(0)) "]" |
PRINT "[" LEFT$(FNLexOrder(0)) "]" |
||
END |
END |
||
Line 312: | Line 312: | ||
IF i% > 0 s$+=STR$i% + "," + FNLexOrder(i% * 10) |
IF i% > 0 s$+=STR$i% + "," + FNLexOrder(i% * 10) |
||
NEXT |
NEXT |
||
=s$</ |
=s$</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1,10,11,12,13,2,3,4,5,6,7,8,9]</pre> |
<pre>[1,10,11,12,13,2,3,4,5,6,7,8,9]</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <math.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 367: | Line 367: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 382: | Line 382: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
{{works with|C sharp|7}} |
{{works with|C sharp|7}} |
||
< |
<syntaxhighlight lang="csharp">using static System.Console; |
||
using static System.Linq.Enumerable; |
using static System.Linq.Enumerable; |
||
Line 392: | Line 392: | ||
public static IEnumerable<int> LexOrder(int n) => (n < 1 ? Range(n, 2 - n) : Range(1, n)).OrderBy(i => i.ToString()); |
public static IEnumerable<int> LexOrder(int n) => (n < 1 ? Range(n, 2 - n) : Range(1, n)).OrderBy(i => i.ToString()); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 403: | Line 403: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <iostream> |
#include <iostream> |
||
#include <numeric> |
#include <numeric> |
||
Line 443: | Line 443: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 455: | Line 455: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(def n 13) |
||
(sort-by str (range 1 (inc n)))</ |
(sort-by str (range 1 (inc n)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(1 10 11 12 13 2 3 4 5 6 7 8 9)</pre> |
<pre>(1 10 11 12 13 2 3 4 5 6 7 8 9)</pre> |
||
Line 462: | Line 462: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
{{works with|GnuCOBOL}} |
{{works with|GnuCOBOL}} |
||
< |
<syntaxhighlight lang="cobol"> identification division. |
||
program-id. LexicographicalNumbers. |
program-id. LexicographicalNumbers. |
||
Line 497: | Line 497: | ||
display "]" |
display "]" |
||
stop run |
stop run |
||
.</ |
.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9]</pre> |
<pre>[1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9]</pre> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun lexicographic-sort (n) |
(defun lexicographic-sort (n) |
||
(sort (alexandria:iota n :start 1) #'string<= :key #'write-to-string)) |
(sort (alexandria:iota n :start 1) #'string<= :key #'write-to-string)) |
||
(lexicographic-sort 13) |
(lexicographic-sort 13) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 512: | Line 512: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: formatting kernel math.parser math.ranges sequences |
||
sorting ; |
sorting ; |
||
IN: rosetta-code.lexicographical-numbers |
IN: rosetta-code.lexicographical-numbers |
||
Line 520: | Line 520: | ||
[ string>number ] map ; |
[ string>number ] map ; |
||
{ 13 21 -22 } [ dup lex-order "%3d: %[%d, %]\n" printf ] each</ |
{ 13 21 -22 } [ dup lex-order "%3d: %[%d, %]\n" printf ] each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 537: | Line 537: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">function leq( n as integer, m as integer ) as boolean |
||
if str(n)<=str(m) then return true else return false |
if str(n)<=str(m) then return true else return false |
||
end function |
end function |
||
Line 577: | Line 577: | ||
if i<n-1 then print ", "; |
if i<n-1 then print ", "; |
||
next i |
next i |
||
print "]"</ |
print "]"</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
? 13 |
? 13 |
||
Line 584: | Line 584: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 614: | Line 614: | ||
fmt.Printf("%3d: %v\n", n, lexOrder(n)) |
fmt.Printf("%3d: %v\n", n, lexOrder(n)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 628: | Line 628: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (sort) |
||
task :: (Ord b, Show b) => [b] -> [b] |
task :: (Ord b, Show b) => [b] -> [b] |
||
task = map snd . sort . map (\i -> (show i, i)) |
task = map snd . sort . map (\i -> (show i, i)) |
||
main = print $ task [1 .. 13]</ |
main = print $ task [1 .. 13]</syntaxhighlight> |
||
Which we could also write, in a point-free style as: |
Which we could also write, in a point-free style as: |
||
< |
<syntaxhighlight lang="haskell">import Data.List (sort) |
||
task |
task |
||
Line 643: | Line 643: | ||
task = map snd . sort . map (show >>= (,)) |
task = map snd . sort . map (show >>= (,)) |
||
main = print $ task [1 .. 13]</ |
main = print $ task [1 .. 13]</syntaxhighlight> |
||
and the simplest approach might be ''sortOn show'' (which only evaluates ''show'' once for each item). |
and the simplest approach might be ''sortOn show'' (which only evaluates ''show'' once for each item). |
||
< |
<syntaxhighlight lang="haskell">import Data.List (sortOn) |
||
main :: IO () |
main :: IO () |
||
main = print $ sortOn show [1 .. 13]</ |
main = print $ sortOn show [1 .. 13]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 658: | Line 658: | ||
{{works with|Isabelle|2020}} |
{{works with|Isabelle|2020}} |
||
< |
<syntaxhighlight lang="isabelle">theory LexList |
||
imports |
imports |
||
Main |
Main |
||
Line 680: | Line 680: | ||
value ‹sort (map ascii_of (upt 1 13))› |
value ‹sort (map ascii_of (upt 1 13))› |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 688: | Line 688: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">task=: [: (/: ":"0) 1 + i. |
||
task 13</ |
task 13</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 698: | Line 698: | ||
<br> |
<br> |
||
Requires Java 8 or later. |
Requires Java 8 or later. |
||
< |
<syntaxhighlight lang="java">import java.util.List; |
||
import java.util.stream.*; |
import java.util.stream.*; |
||
Line 723: | Line 723: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 737: | Line 737: | ||
=={{header|Ksh}}== |
=={{header|Ksh}}== |
||
< |
<syntaxhighlight lang="ksh"> |
||
#!/bin/ksh |
#!/bin/ksh |
||
Line 773: | Line 773: | ||
_fillarray arr ${N} |
_fillarray arr ${N} |
||
print -- ${arr[*]}</ |
print -- ${arr[*]}</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
1 10 11 12 13 2 3 4 5 6 7 8 9</pre> |
1 10 11 12 13 2 3 4 5 6 7 8 9</pre> |
||
Line 780: | Line 780: | ||
{{works with|jq}} |
{{works with|jq}} |
||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
< |
<syntaxhighlight lang="jq">def sort_range($a;$b): [range($a;$b)] | sort_by(tostring); |
||
# Example |
# Example |
||
# jq's index origin is 0, so ... |
# jq's index origin is 0, so ... |
||
sort_range(1;14)</ |
sort_range(1;14)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 791: | Line 791: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">lexorderedsequence(n) = sort(collect(n > 0 ? (1:n) : n:1), lt=(a,b) -> string(a) < string(b)) |
||
for i in [0, 5, 13, 21, -32] |
for i in [0, 5, 13, 21, -32] |
||
println(lexorderedsequence(i)) |
println(lexorderedsequence(i)) |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
[0, 1] |
[0, 1] |
||
Line 806: | Line 806: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// Version 1.2.51 |
||
fun lexOrder(n: Int): List<Int> { |
fun lexOrder(n: Int): List<Int> { |
||
Line 823: | Line 823: | ||
println("${"%3d".format(n)}: ${lexOrder(n)}") |
println("${"%3d".format(n)}: ${lexOrder(n)}") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 837: | Line 837: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
1) lexicographically sorting a sequence of numbers |
1) lexicographically sorting a sequence of numbers |
||
{S.sort before 1 2 3 4 5 6 7 8 9 10 11 12 13} |
{S.sort before 1 2 3 4 5 6 7 8 9 10 11 12 13} |
||
Line 845: | Line 845: | ||
{A.sort! before {A.new 1 2 3 4 5 6 7 8 9 10 11 12 13}} |
{A.sort! before {A.new 1 2 3 4 5 6 7 8 9 10 11 12 13}} |
||
-> [1,10,11,12,13,2,3,4,5,6,7,8,9] |
-> [1,10,11,12,13,2,3,4,5,6,7,8,9] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Lua's in-built table.sort function will sort a table of strings into lexicographical order by default. This task therefore becomes trivial by converting each number to a string before adding it to the table. |
Lua's in-built table.sort function will sort a table of strings into lexicographical order by default. This task therefore becomes trivial by converting each number to a string before adding it to the table. |
||
< |
<syntaxhighlight lang="lua">function lexNums (limit) |
||
local numbers = {} |
local numbers = {} |
||
for i = 1, limit do |
for i = 1, limit do |
||
Line 859: | Line 859: | ||
local numList = lexNums(13) |
local numList = lexNums(13) |
||
print(table.concat(numList, " "))</ |
print(table.concat(numList, " "))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre> |
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module Checkit { |
Module Checkit { |
||
Function lexicographical(N) { |
Function lexicographical(N) { |
||
Line 917: | Line 917: | ||
} |
} |
||
Checkit |
Checkit |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
<lang |
<syntaxhighlight lang="mathematica">SortBy[Range[13],ToString]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}</pre> |
<pre>{1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}</pre> |
||
Line 926: | Line 926: | ||
=={{header|Microsoft Small Basic}}== |
=={{header|Microsoft Small Basic}}== |
||
In Small Basic there is no string comparison: “a”>”b” the result is “False”, “b”>”a” the result is also “False”. It doesn’t help at all. |
In Small Basic there is no string comparison: “a”>”b” the result is “False”, “b”>”a” the result is also “False”. It doesn’t help at all. |
||
< |
<syntaxhighlight lang="smallbasic">' Lexicographical numbers - 25/07/2018 |
||
xx="000000000000000" |
xx="000000000000000" |
||
For n=1 To 3 |
For n=1 To 3 |
||
Line 959: | Line 959: | ||
EndFor |
EndFor |
||
TextWindow.WriteLine(nn+":"+Text.GetSubTextToEnd(x,2)) |
TextWindow.WriteLine(nn+":"+Text.GetSubTextToEnd(x,2)) |
||
EndFor </ |
EndFor </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
5:1,2,3,4,5 |
5:1,2,3,4,5 |
||
Line 975: | Line 975: | ||
- The condensed version shows that there are no reserved keywords |
- The condensed version shows that there are no reserved keywords |
||
<syntaxhighlight lang="mumps"> |
|||
<lang MUMPS> |
|||
SortLexographically(n) |
SortLexographically(n) |
||
new array,i,j |
new array,i,j |
||
Line 981: | Line 981: | ||
for set j=$order(array(j)) quit:j="" write j |
for set j=$order(array(j)) quit:j="" write j |
||
quit |
quit |
||
</syntaxhighlight> |
|||
</lang> |
|||
This could also be written: |
This could also be written: |
||
<syntaxhighlight lang="mumps"> |
|||
<lang MUMPS> |
|||
SortLexographically(n) n a,i,j f i=1:1:n s a(i_" ")="" |
SortLexographically(n) n a,i,j f i=1:1:n s a(i_" ")="" |
||
f s j=$o(a(j)) q:j="" w j |
f s j=$o(a(j)) q:j="" w j |
||
q |
q |
||
</syntaxhighlight> |
|||
</lang> |
|||
;Usage |
;Usage |
||
<lang |
<syntaxhighlight lang="mumps"> do SortLexographically(13)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,001: | Line 1,001: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import algorithm, sequtils |
||
for n in [0, 5, 13, 21, -22]: |
for n in [0, 5, 13, 21, -22]: |
||
let s = if n > 1: toSeq(1..n) else: toSeq(countdown(1, n)) |
let s = if n > 1: toSeq(1..n) else: toSeq(countdown(1, n)) |
||
echo s.sortedByIt($it)</ |
echo s.sortedByIt($it)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,015: | Line 1,015: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">printf("%4d: [%s]\n", $_, join ',', sort $_ > 0 ? 1..$_ : $_..1) for 13, 21, -22</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 13: [1,10,11,12,13,2,3,4,5,6,7,8,9] |
<pre> 13: [1,10,11,12,13,2,3,4,5,6,7,8,9] |
||
Line 1,023: | Line 1,023: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Accepts a proper sequence, in preference to guessing what say a lone 13 actually means, and/or wanting start/stop/step that'd probably just get passed on to tagset() anyway. |
Accepts a proper sequence, in preference to guessing what say a lone 13 actually means, and/or wanting start/stop/step that'd probably just get passed on to tagset() anyway. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">lexicographic_order</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">lexicographic_order</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
||
Line 1,035: | Line 1,035: | ||
<span style="color: #0000FF;">?</span><span style="color: #000000;">lexicographic_order</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">))</span> |
<span style="color: #0000FF;">?</span><span style="color: #000000;">lexicographic_order</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">))</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #000000;">lexicographic_order</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1.25</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">11.3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">13</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">})</span> |
<span style="color: #0000FF;">?</span><span style="color: #000000;">lexicographic_order</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1.25</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">11.3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">13</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,047: | Line 1,047: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(println |
||
(by |
(by |
||
format |
format |
||
sort |
sort |
||
(range 1 13) ) )</ |
(range 1 13) ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,059: | Line 1,059: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
< |
<syntaxhighlight lang="prolog">lexicographical_sort(Numbers, Sorted_numbers):- |
||
number_strings(Numbers, Strings), |
number_strings(Numbers, Strings), |
||
sort(Strings, Sorted_strings), |
sort(Strings, Sorted_strings), |
||
Line 1,093: | Line 1,093: | ||
test(13), |
test(13), |
||
test(21), |
test(21), |
||
test(-22).</ |
test(-22).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,106: | Line 1,106: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="purebasic">EnableExplicit |
||
Procedure lexOrder(n, Array ints(1)) |
Procedure lexOrder(n, Array ints(1)) |
||
Line 1,149: | Line 1,149: | ||
Data.i 0, 5, 13, 21, -22 |
Data.i 0, 5, 13, 21, -22 |
||
EndDataSection |
EndDataSection |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,163: | Line 1,163: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">n=13 |
||
print(sorted(range(1,n+1), key=str))</ |
print(sorted(range(1,n+1), key=str))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9] |
<pre>[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9] |
||
Line 1,171: | Line 1,171: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ [] swap times |
||
[ i^ 1+ number$ |
[ i^ 1+ number$ |
||
nested join ] |
nested join ] |
||
Line 1,179: | Line 1,179: | ||
[ $->n drop join ] ] is task ( n --> [ ) |
[ $->n drop join ] ] is task ( n --> [ ) |
||
13 task echo</ |
13 task echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,188: | Line 1,188: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (lex-sort n) (sort (if (< 0 n) (range 1 (add1 n)) (range n 2)) |
(define (lex-sort n) (sort (if (< 0 n) (range 1 (add1 n)) (range n 2)) |
||
Line 1,200: | Line 1,200: | ||
(show 13) |
(show 13) |
||
(show 21) |
(show 21) |
||
(show -22)</ |
(show -22)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,216: | Line 1,216: | ||
{{works with|Rakudo|2018.06}} |
{{works with|Rakudo|2018.06}} |
||
It is somewhat odd that the task name is sort '''numbers''' lexicographically but immediately backtracks in the task header to sorting '''integers''' lexicographically. Why only integers? This will sort ANY real numbers lexicographically. For a non-integer, assumes that the given number is a hard boundary and 1 is a "soft" boundary. E.G. The given number is definitely included; 1 is only a threshold, it is included if it matches exactly. (Could be the other way around, this it the way I choose.) |
It is somewhat odd that the task name is sort '''numbers''' lexicographically but immediately backtracks in the task header to sorting '''integers''' lexicographically. Why only integers? This will sort ANY real numbers lexicographically. For a non-integer, assumes that the given number is a hard boundary and 1 is a "soft" boundary. E.G. The given number is definitely included; 1 is only a threshold, it is included if it matches exactly. (Could be the other way around, this it the way I choose.) |
||
<lang |
<syntaxhighlight lang="raku" line>sub lex (Real $n, $step = 1) { |
||
($n < 1 ?? ($n, * + $step …^ * > 1) |
($n < 1 ?? ($n, * + $step …^ * > 1) |
||
!! ($n, * - $step …^ * < 1)).sort: ~* |
!! ($n, * - $step …^ * < 1)).sort: ~* |
||
Line 1,225: | Line 1,225: | ||
my ($bound, $step) = |$_, 1; |
my ($bound, $step) = |$_, 1; |
||
say "Boundary:$bound, Step:$step >> ", lex($bound, $step).join: ', '; |
say "Boundary:$bound, Step:$step >> ", lex($bound, $step).join: ', '; |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Boundary:13, Step:1 >> 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9 |
<pre>Boundary:13, Step:1 >> 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9 |
||
Line 1,237: | Line 1,237: | ||
This REXX version allows the starting and ending numbers to be specified via the command line (CL), |
This REXX version allows the starting and ending numbers to be specified via the command line (CL), |
||
<br>as well as the increment. Negative numbers are supported and need not be integers. |
<br>as well as the increment. Negative numbers are supported and need not be integers. |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm displays a horizontal list of a range of numbers sorted lexicographically.*/ |
||
parse arg LO HI INC . /*obtain optional arguments from the CL*/ |
parse arg LO HI INC . /*obtain optional arguments from the CL*/ |
||
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/ |
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/ |
||
Line 1,260: | Line 1,260: | ||
do j=1 for m; k= j+1; if @.j>>@.k then parse value @.j @.k 0 with @.k @.j ok |
do j=1 for m; k= j+1; if @.j>>@.k then parse value @.j @.k 0 with @.k @.j ok |
||
end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/ |
end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/ |
||
end /*m*/; return</ |
end /*m*/; return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,283: | Line 1,283: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Lexicographical numbers |
# Project : Lexicographical numbers |
||
Line 1,303: | Line 1,303: | ||
svect = left(svect, len(svect) - 1) |
svect = left(svect, len(svect) - 1) |
||
see svect + "]" + nl |
see svect + "]" + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,310: | Line 1,310: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">n = 13 |
||
p (1..n).sort_by(&:to_s) |
p (1..n).sort_by(&:to_s) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9] |
<pre>[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9] |
||
Line 1,318: | Line 1,318: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn lex_sorted_vector(num: i32) -> Vec<i32> { |
||
let (min, max) = if num >= 1 { (1, num) } else { (num, 1) }; |
let (min, max) = if num >= 1 { (1, num) } else { (num, 1) }; |
||
let mut str: Vec<String> = (min..=max).map(|i| i.to_string()).collect(); |
let mut str: Vec<String> = (min..=max).map(|i| i.to_string()).collect(); |
||
Line 1,329: | Line 1,329: | ||
println!("{}: {:?}", n, lex_sorted_vector(*n)); |
println!("{}: {:?}", n, lex_sorted_vector(*n)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,342: | Line 1,342: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/KpWHYNR/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/BnxJXLjCRvObdOv3VKTtYA Scastie (remote JVM)]. |
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/KpWHYNR/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/BnxJXLjCRvObdOv3VKTtYA Scastie (remote JVM)]. |
||
< |
<syntaxhighlight lang="scala">object LexicographicalNumbers extends App { def ints = List(0, 5, 13, 21, -22) |
||
def lexOrder(n: Int): Seq[Int] = (if (n < 1) n to 1 else 1 to n).sortBy(_.toString) |
def lexOrder(n: Int): Seq[Int] = (if (n < 1) n to 1 else 1 to n).sortBy(_.toString) |
||
Line 1,349: | Line 1,349: | ||
for (n <- ints) println(f"$n%3d: ${lexOrder(n).mkString("[",", ", "]")}%s") |
for (n <- ints) println(f"$n%3d: ${lexOrder(n).mkString("[",", ", "]")}%s") |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func lex_order (n) { |
||
[range(1, n, n.sgn)...].sort_by { Str(_) } |
[range(1, n, n.sgn)...].sort_by { Str(_) } |
||
} |
} |
||
Line 1,358: | Line 1,358: | ||
[13, 21, -22].each {|n| |
[13, 21, -22].each {|n| |
||
printf("%4s: %s\n", n, lex_order(n)) |
printf("%4s: %s\n", n, lex_order(n)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,369: | Line 1,369: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func lex(n: Int) -> [Int] { |
||
return stride(from: 1, through: n, by: n.signum()).map({ String($0) }).sorted().compactMap(Int.init) |
return stride(from: 1, through: n, by: n.signum()).map({ String($0) }).sorted().compactMap(Int.init) |
||
} |
} |
||
Line 1,375: | Line 1,375: | ||
print("13: \(lex(n: 13))") |
print("13: \(lex(n: 13))") |
||
print("21: \(lex(n: 21))") |
print("21: \(lex(n: 21))") |
||
print("-22: \(lex(n: -22))")</ |
print("-22: \(lex(n: -22))")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,384: | Line 1,384: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc iota {num {start 0} {step 1}} { |
||
set res {} |
set res {} |
||
set end [+ $start [* $step $num]] |
set end [+ $start [* $step $num]] |
||
Line 1,393: | Line 1,393: | ||
} |
} |
||
puts [lsort [iota 13 1]]</ |
puts [lsort [iota 13 1]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre> |
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Public Function sortlexicographically(N As Integer) |
||
Dim arrList As Object |
Dim arrList As Object |
||
Set arrList = CreateObject("System.Collections.ArrayList") |
Set arrList = CreateObject("System.Collections.ArrayList") |
||
Line 1,413: | Line 1,413: | ||
Public Sub main() |
Public Sub main() |
||
Call sortlexicographically(13) |
Call sortlexicographically(13) |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9, </pre> |
<pre>1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9, </pre> |
||
Line 1,419: | Line 1,419: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-sort}} |
{{libheader|Wren-sort}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/sort" for Sort |
||
var a = (1..13).map { |i| "%(i)" }.toList |
var a = (1..13).map { |i| "%(i)" }.toList |
||
Sort.quick(a) |
Sort.quick(a) |
||
System.print(a)</ |
System.print(a)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,431: | Line 1,431: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn lexN(n){ n.pump(List,'+(1),"toString").sort().apply("toInt") }</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">foreach n in (T(5,13,21)){ println("%2d: %s".fmt(n,lexN(n).concat(","))) }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |