Sort numbers lexicographically: Difference between revisions

m
(add FreeBASIC)
m (→‎{{header|Wren}}: Minor tidy)
 
(14 intermediate revisions by 10 users not shown)
Line 18:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V n = 13
print(sorted(Array(1..n), key' i -> String(i)))</langsyntaxhighlight>
 
{{out}}
Line 27:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC PrintArray(INT ARRAY a INT size)
INT i
 
Line 86:
Test(a,COUNT_A)
Test(b,COUNT_B)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_numbers_lexicographically.png Screenshot from Atari 8-bit computer]
Line 102:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">WITH Ada.Containers.Generic_Array_Sort, Ada.Text_IO;
USE Ada.Text_IO;
PROCEDURE Main IS
Line 121:
Show (21);
END Main;
</syntaxhighlight>
</lang>
 
{{out}}
Line 129:
</pre>
{{out}}
 
=={{header|ALGOL 68}}==
Uses code from the [[Sorting algorithms/Insertion sort]] tasl - included here for convenience.<br>
<syntaxhighlight lang="algol68">
BEGIN # sort numbers lexicographically #
 
# code from the Sorting algorithms/Insertion sort task #
MODE DATA = STRING;
 
PROC in place insertion sort = (REF[]DATA item)VOID:
BEGIN
INT first := LWB item;
INT last := UPB item;
INT j;
DATA value;
FOR i FROM first + 1 TO last DO
value := item[i];
j := i - 1;
WHILE ( j >= LWB item AND j <= UPB item | item[j]>value | FALSE ) DO
item[j + 1] := item[j];
j -:= 1
OD;
item[j + 1] := value
OD
END # in place insertion sort #;
 
# end code from the Sorting algorithms/Insertion sort task #
 
# returns s converted to an integer, NB: no error checking #
OP TOINT = ( STRING s )INT:
BEGIN
INT result := 0;
FOR i FROM LWB s TO UPB s DO
result *:= 10 +:= ( ABS s[ i ] - ABS "0" )
OD;
result
END # TOINT # ;
 
# returns a array of integers 1..n sorted lexicographically #
PROC lexicographic order = ( INT n )[]INT:
BEGIN
[ 1 : n ]STRING v; FOR i TO n DO v[ i ] := whole( i, 0 ) OD;
in place insertion sort( v );
[ 1 : n ]INT result;
FOR i TO n DO result[ i ] := TOINT v[ i ] OD;
result
END # lexicographic order # ;
 
# prints the elements of a #
PROC show int array = ( []INT a )VOID:
BEGIN
print( ( "[" ) );
FOR i FROM LWB a TO UPB a DO print( ( " ", whole( a[ i ], 0 ) ) ) OD;
print( ( " ]", newline ) )
END # show int array # ;
 
# test cases #
show int array( lexicographic order( 13 ) );
show int array( lexicographic order( 21 ) )
 
END
</syntaxhighlight>
{{out}}
<pre>
[ 1 10 11 12 13 2 3 4 5 6 7 8 9 ]
[ 1 10 11 12 13 14 15 16 17 18 19 2 20 21 3 4 5 6 7 8 9 ]
</pre>
 
=={{header|APL}}==
Line 143 ⟶ 210:
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:
 
<langsyntaxhighlight lang="applescript">on oneToNLexicographically(n)
script o
property output : {}
Line 167 ⟶ 234:
 
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}</langsyntaxhighlight>
 
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 AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later.
<lang applescript>use sorter : script "Custom Iterative Ternary Merge Sort" -- <https://macscripter.net/viewtopic.php?pid=194430#p194430>
use sorter : script ¬
"Custom Iterative Ternary Merge Sort" -- <www.macscripter.net/t/timsort-and-nigsort/71383/3>
 
on join(lst, delim)
on sortLexicographically(integerList) -- Sorts integerList in place.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeeddelim
set textListtxt to paragraphs of (integerListlst as text)
set AppleScript's text item delimiters to astid
return txt
end join
 
on sortLexicographically(integerList)
set textList to paragraphs of join(integerList, linefeed)
-- Sort textList, echoing the moves in integerList.
considering hyphens but ignoring numeric strings
tell sorter to sort(textList, 1, -1, {slave:{integerList}})
Line 185 ⟶ 259:
 
-- Test code:
local someIntegers
set someIntegers to {1, 2, -6, 3, 4, 5, -10, 6, 7, 8, 9, 10, 11, 12, 13, -2, -5, -1, -4, -3, 0}
sortLexicographically(someIntegers)
return someIntegers</syntaxhighlight>
 
--> {-1, -10, -2, -3, -4, -5, -6, 0, 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}</lang>
{{output}}
<syntaxhighlight lang="applescript">{-1, -10, -2, -3, -4, -5, -6, 0, 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}</syntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">arr: 1..13
print sort map arr => [to :string &]</langsyntaxhighlight>
 
{{out}}
 
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre>
 
=={{header|ATS}}==
 
The program converts the integers to strings, sorts the strings, then converts the strings back to integers.
The method can be used to sort any sequence of non-negative integers.
 
(Radix sorting the numbers in their original form is another possible approach.)
 
<syntaxhighlight lang="ats">
(* The Rosetta Code lexicographic sort task. *)
 
#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"
 
#define NIL list_nil ()
#define :: list_cons
 
fn
uint_to_digit (n : uint)
: char =
int2char0 (g0u2i n + char2int0 '0')
 
fn
int_to_string {n : nat}
(n : int n)
: string =
if iseqz n then
"0"
else
let
fun
loop {i : nat | i <= 20}
.<i>.
(buf : &array (char, 21),
i : int i,
n : uint)
: [j : nat | j <= 20]
int j =
if (i = 0) + (iseqz n) then
i
else
let
val i1 = pred i
in
buf[i1] := uint_to_digit (n mod 10u);
loop (buf, i1, n / 10u)
end
 
var buf = @[char][21] ('\0')
val j = loop (buf, 20, g0i2u n)
val p = ptr_add<char> (addr@ buf, j)
in
strptr2string (string0_copy ($UN.cast{string} p))
end
 
fn
iota1 {n : pos}
(n : int n)
: list ([i : pos | i <= n] int i, n) =
let
typedef t = [i : pos | i <= n] int i
 
fun
loop {i : nat | i <= n}
.<i>.
(i : int i,
accum : list (t, n - i))
: list (t, n) =
if i = 0 then
accum
else
loop (pred i, i :: accum)
in
loop (n, NIL)
end
 
fn
reverse_map_numbers_to_strings
{n : int}
(nums : list ([i : nat] int i, n))
: list (string, n) =
let
typedef t = [i : nat] int i
 
fun
loop {i : nat | i <= n}
.<n - i>.
(nums : list (t, n - i),
accum : list (string, i))
: list (string, n) =
case+ nums of
| NIL => accum
| head :: tail =>
loop {i + 1} (tail, int_to_string head :: accum)
 
prval () = lemma_list_param nums
in
loop {0} (nums, NIL)
end
 
fn
reverse_map_strings_to_numbers
{n : int}
(strings : list (string, n))
: list (int, n) =
let
macdef string_to_int (s) =
$extfcall (int, "atoi", ,(s))
 
fun
loop {i : nat | i <= n}
.<n - i>.
(strings : list (string, n - i),
accum : list (int, i))
: list (int, n) =
case+ strings of
| NIL => accum
| head :: tail =>
loop {i + 1} (tail, string_to_int head :: accum)
 
prval () = lemma_list_param strings
in
loop {0} (strings, NIL)
end
fn
lexicographic_iota1
{n : pos}
(n : int n)
: list (int, n) =
let
val numstrings =
reverse_map_numbers_to_strings (iota1 n)
 
(* One could use a MSB-first radix sort here, but I will use what
is readily available. *)
implement
list_mergesort$cmp<string> (x, y) =
~compare (x, y)
in
reverse_map_strings_to_numbers
(list_vt2t (list_mergesort<string> numstrings))
end
 
implement
main0 () =
begin
println! (lexicographic_iota1 13);
println! (lexicographic_iota1 100)
end
</syntaxhighlight>
 
{{out}}
<pre>$ patscc -DATS_MEMALLOC_GCBDW lexicographic_sort.dats -lgc && ./a.out
1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9
1, 10, 100, 11, 12, 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</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">n2lexicog(n){
Arr := [], list := ""
loop % n
Line 208 ⟶ 441:
Arr.Push(v)
return Arr
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">n := 13
x := n2lexicog(n)
for k, v in x
output .= v ", "
MsgBox % "[" Trim(output, ", ") "]" ; show output
return</langsyntaxhighlight>
{{out}}
<pre>[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]</pre>
Line 220 ⟶ 453:
=={{header|AWK}}==
===Robust with checks===
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SORT_NUMBERS_LEXICOGRAPHICALLY.AWK
#
Line 265 ⟶ 498:
return(str)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 279 ⟶ 512:
===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.
<langsyntaxhighlight AWKlang="awk">BEGIN {
n=13
for (i=1; i<=n; i++)
Line 286 ⟶ 519:
for (k in a)
printf "%d ", a[k]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 293 ⟶ 526:
=={{header|BaCon}}==
Create a delimited string with numbers and use SORT$.
<langsyntaxhighlight lang="bacon">CONST n = 13
FOR x = 1 TO n
result$ = APPEND$(result$, 0, STR$(x))
NEXT
PRINT SORT$(result$)</langsyntaxhighlight>
{{out}}
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre>
Line 303 ⟶ 536:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> N%=13
PRINT "[" LEFT$(FNLexOrder(0)) "]"
END
Line 312 ⟶ 545:
IF i% > 0 s$+=STR$i% + "," + FNLexOrder(i% * 10)
NEXT
=s$</langsyntaxhighlight>
{{out}}
<pre>[1,10,11,12,13,2,3,4,5,6,7,8,9]</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Task ← ⍋∘(•Fmt¨)⊸⊏1+↕
 
Task 13</syntaxhighlight>
{{out}}
<pre>⟨ 1 10 11 12 13 2 3 4 5 6 7 8 9 ⟩</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Line 367 ⟶ 607:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
<pre>
Line 382 ⟶ 621:
=={{header|C sharp}}==
{{works with|C sharp|7}}
<langsyntaxhighlight lang="csharp">using static System.Console;
using static System.Linq.Enumerable;
 
Line 392 ⟶ 631:
 
public static IEnumerable<int> LexOrder(int n) => (n < 1 ? Range(n, 2 - n) : Range(1, n)).OrderBy(i => i.ToString());
}</langsyntaxhighlight>
{{out}}
<pre>
Line 403 ⟶ 642:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <numeric>
Line 443 ⟶ 682:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 455 ⟶ 694:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(def n 13)
(sort-by str (range 1 (inc n)))</langsyntaxhighlight>
{{out}}
<pre>(1 10 11 12 13 2 3 4 5 6 7 8 9)</pre>
Line 462 ⟶ 701:
=={{header|COBOL}}==
{{works with|GnuCOBOL}}
<langsyntaxhighlight lang="cobol"> identification division.
program-id. LexicographicalNumbers.
 
Line 497 ⟶ 736:
display "]"
stop run
.</langsyntaxhighlight>
{{out}}
<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}}==
<langsyntaxhighlight lang="lisp">
(defun lexicographic-sort (n)
(sort (alexandria:iota n :start 1) #'string<= :key #'write-to-string))
(lexicographic-sort 13)
</syntaxhighlight>
</lang>
 
{{out}}
Line 512 ⟶ 751:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting kernel math.parser math.ranges sequences
sorting ;
IN: rosetta-code.lexicographical-numbers
Line 520 ⟶ 759:
[ string>number ] map ;
{ 13 21 -22 } [ dup lex-order "%3d: %[%d, %]\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 530 ⟶ 769:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sort_numbers_lexicographically}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Sort numbers lexicographically 01.png]]
In '''[https://formulae.org/?example=Sort_numbers_lexicographically this]''' page you can see the program(s) related to this task and their results.
 
'''Test case'''
 
[[File:Fōrmulæ - Sort numbers lexicographically 02.png]]
 
[[File:Fōrmulæ - Sort numbers lexicographically 03.png]]
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function leq( n as integer, m as integer ) as boolean
if str(n)<=str(m) then return true else return false
end function
Line 577 ⟶ 822:
if i<n-1 then print ", ";
next i
print "]"</langsyntaxhighlight>
{{out}}<pre>
? 13
Line 584 ⟶ 829:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 614 ⟶ 859:
fmt.Printf("%3d: %v\n", n, lexOrder(n))
}
}</langsyntaxhighlight>
 
{{out}}
Line 628 ⟶ 873:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.List (sort)
 
task :: (Ord b, Show b) => [b] -> [b]
task = map snd . sort . map (\i -> (show i, i))
 
main = print $ task [1 .. 13]</langsyntaxhighlight>
 
Which we could also write, in a point-free style as:
<langsyntaxhighlight lang="haskell">import Data.List (sort)
 
task
Line 643 ⟶ 888:
task = map snd . sort . map (show >>= (,))
 
main = print $ task [1 .. 13]</langsyntaxhighlight>
 
and the simplest approach might be ''sortOn show'' (which only evaluates ''show'' once for each item).
 
<langsyntaxhighlight lang="haskell">import Data.List (sortOn)
 
main :: IO ()
main = print $ sortOn show [1 .. 13]</langsyntaxhighlight>
 
{{out}}
Line 658 ⟶ 903:
{{works with|Isabelle|2020}}
 
<langsyntaxhighlight Isabellelang="isabelle">theory LexList
imports
Main
Line 680 ⟶ 925:
 
value ‹sort (map ascii_of (upt 1 13))›
end</langsyntaxhighlight>
 
{{out}}
Line 688 ⟶ 933:
=={{header|J}}==
 
<langsyntaxhighlight Jlang="j">task=: [: (/: ":"0) 1 + i.
task 13</langsyntaxhighlight>
 
{{out}}
Line 698 ⟶ 943:
<br>
Requires Java 8 or later.
<langsyntaxhighlight lang="java">import java.util.List;
import java.util.stream.*;
 
Line 723 ⟶ 968:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 735 ⟶ 980:
-22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1]
</pre>
 
=={{header|K}}==
<syntaxhighlight lang="k">task: 1+<$1+!:
 
task 13</syntaxhighlight>
{{out}}
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre>
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 773 ⟶ 1,025:
_fillarray arr ${N}
 
print -- ${arr[*]}</langsyntaxhighlight>
{{out}}<pre>
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq">def sort_range($a;$b): [range($a;$b)] | sort_by(tostring);
 
# Example
# jq's index origin is 0, so ...
sort_range(1;14)</langsyntaxhighlight>
{{out}}
<pre>
Line 791 ⟶ 1,043:
 
=={{header|Julia}}==
<langsyntaxhighlight 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]
println(lexorderedsequence(i))
end
</langsyntaxhighlight>{{out}}
<pre>
[0, 1]
Line 806 ⟶ 1,058:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// Version 1.2.51
 
fun lexOrder(n: Int): List<Int> {
Line 823 ⟶ 1,075:
println("${"%3d".format(n)}: ${lexOrder(n)}")
}
}</langsyntaxhighlight>
 
{{output}}
Line 837 ⟶ 1,089:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
1) lexicographically sorting a sequence of numbers
{S.sort before 1 2 3 4 5 6 7 8 9 10 11 12 13}
Line 845 ⟶ 1,097:
{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]
</syntaxhighlight>
</lang>
 
=={{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.
<langsyntaxhighlight lang="lua">function lexNums (limit)
local numbers = {}
for i = 1, limit do
Line 859 ⟶ 1,111:
 
local numList = lexNums(13)
print(table.concat(numList, " "))</langsyntaxhighlight>
{{out}}
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Function lexicographical(N) {
Line 917 ⟶ 1,169:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">SortBy[Range[13],ToString]</langsyntaxhighlight>
{{out}}
<pre>{1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}</pre>
Line 926 ⟶ 1,178:
=={{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.
<langsyntaxhighlight lang="smallbasic">' Lexicographical numbers - 25/07/2018
xx="000000000000000"
For n=1 To 3
Line 959 ⟶ 1,211:
EndFor
TextWindow.WriteLine(nn+":"+Text.GetSubTextToEnd(x,2))
EndFor </langsyntaxhighlight>
{{out}}
5:1,2,3,4,5
13:1,10,11,12,13,2,3,4,5,6,7,8,9
21:1,10,11,12,13,14,15,16,17,18,19,2,20,21,3,4,5,6,7,8,9
 
=={{header|MiniScript}}==
Output from REPL.
{{out}}
<pre>
> n = 13
> rng = range(1, 13)
> rng.join(" ").split(" ").sort
["1", "10", "11", "12", "13", "2", "3", "4", "5", "6", "7", "8", "9"]
</pre>
 
=={{header|MUMPS}}==
Line 975 ⟶ 1,237:
- The condensed version shows that there are no reserved keywords
 
<syntaxhighlight lang="mumps">
<lang MUMPS>
SortLexographically(n)
new array,i,j
Line 981 ⟶ 1,243:
for set j=$order(array(j)) quit:j="" write j
quit
</syntaxhighlight>
</lang>
 
This could also be written:
 
<syntaxhighlight lang="mumps">
<lang MUMPS>
SortLexographically(n) n a,i,j f i=1:1:n s a(i_" ")=""
f s j=$o(a(j)) q:j="" w j
q
</syntaxhighlight>
</lang>
 
;Usage
 
<syntaxhighlight lang MUMPS="mumps"> do SortLexographically(13)</langsyntaxhighlight>
 
{{out}}
Line 1,001 ⟶ 1,263:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils
 
for n in [0, 5, 13, 21, -22]:
let s = if n > 1: toSeq(1..n) else: toSeq(countdown(1, n))
echo s.sortedByIt($it)</langsyntaxhighlight>
 
{{out}}
Line 1,015 ⟶ 1,277:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">printf("%4d: [%s]\n", $_, join ',', sort $_ > 0 ? 1..$_ : $_..1) for 13, 21, -22</langsyntaxhighlight>
{{out}}
<pre> 13: [1,10,11,12,13,2,3,4,5,6,7,8,9]
Line 1,022 ⟶ 1,284:
 
=={{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.
Idiomatic version - crashes if n<1, and calls sprint() 76 times.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function lexographic(integer i, j)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
return compare(sprint(i),sprint(j))
<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>
end function
<span style="color: #008080;">return</span> <span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))))</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function lex_order(integer n)
return custom_sort(routine_id("lexographic"), tagset(n))
<span style="color: #0000FF;">?</span><span style="color: #000000;">lexicographic_order</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span>
end function
<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;">5</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;">13</span><span style="color: #0000FF;">))</span>
?lex_order(13)</lang>
<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;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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;">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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
{0,1}
{1,2,3,4,5}
{1,10,11,12,13,2,3,4,5,6,7,8,9}
{1,11,13,15,17,19,21,3,5,7,9}
</pre>
{-1,-13,-17,-21,-5,-9,11,3,7}
Alternative version, handles n<1, and for n=13 (say) it calls sprint() only 13 times instead of 76.
{-10,-11.3,-3,-6,0,1.25,13,9}
<lang Phix>function lex_order(integer n)
integer {lo,hi} = iff(n<1?{n-1,1}:{0,n}), l = hi-lo
sequence s = repeat(0,l)
for i=1 to l do s[i] = {sprint(lo+i),lo+i} end for
s = sort(s)
for i=1 to l do s[i] = s[i][2] end for
return s
end function
 
?lex_order(13)
?lex_order(0)
?lex_order(-22)</lang>
{{out}}
<pre>
{1,10,11,12,13,2,3,4,5,6,7,8,9}
{0,1}
{-1,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-2,-20,-21,-22,-3,-4,-5,-6,-7,-8,-9,0,1}
</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(println
(by
format
sort
(range 1 13) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 1,069 ⟶ 1,321:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">lexicographical_sort(Numbers, Sorted_numbers):-
number_strings(Numbers, Strings),
sort(Strings, Sorted_strings),
Line 1,103 ⟶ 1,355:
test(13),
test(21),
test(-22).</langsyntaxhighlight>
 
{{out}}
Line 1,116 ⟶ 1,368:
=={{header|PureBasic}}==
{{trans|Go}}
<langsyntaxhighlight lang="purebasic">EnableExplicit
 
Procedure lexOrder(n, Array ints(1))
Line 1,159 ⟶ 1,411:
Data.i 0, 5, 13, 21, -22
EndDataSection
EndIf</langsyntaxhighlight>
 
{{out}}
Line 1,173 ⟶ 1,425:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">n=13
print(sorted(range(1,n+1), key=str))</langsyntaxhighlight>
{{out}}
<pre>[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]
Line 1,181 ⟶ 1,433:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ [] swap times
[ i^ 1+ number$
nested join ]
Line 1,189 ⟶ 1,441:
[ $->n drop join ] ] is task ( n --> [ )
 
13 task echo</langsyntaxhighlight>
 
{{out}}
Line 1,198 ⟶ 1,450:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define (lex-sort n) (sort (if (< 0 n) (range 1 (add1 n)) (range n 2))
Line 1,210 ⟶ 1,462:
(show 13)
(show 21)
(show -22)</langsyntaxhighlight>
 
{{out}}
Line 1,226 ⟶ 1,478:
{{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.)
<syntaxhighlight lang="raku" perl6line>sub lex (Real $n, $step = 1) {
($n < 1 ?? ($n, * + $step …^ * > 1)
!! ($n, * - $step …^ * < 1)).sort: ~*
Line 1,235 ⟶ 1,487:
my ($bound, $step) = |$_, 1;
say "Boundary:$bound, Step:$step >> ", lex($bound, $step).join: ', ';
}</langsyntaxhighlight>
{{Out}}
<pre>Boundary:13, Step:1 >> 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9
Line 1,247 ⟶ 1,499:
This REXX version allows the starting and ending numbers to be specified via the command line (CL),
<br>as well as the increment. &nbsp; Negative numbers are supported and need not be integers.
<langsyntaxhighlight 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*/
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/
Line 1,270 ⟶ 1,522:
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 /*m*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,293 ⟶ 1,545:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Lexicographical numbers
 
Line 1,313 ⟶ 1,565:
svect = left(svect, len(svect) - 1)
see svect + "]" + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Lexicographical numbers = [1,10,11,12,13,2,3,4,5,6,7,8,9]
</pre>
 
=={{header|RPL}}==
{{works with|HP|48}}
≪ ≪ n →STR ≫ 'n' 1 4 ROLL 1 SEQ
SORT ≪ STR→ ≫ DOLIST
≫ '<span style="color:blue">LEXICON</span>' STO
 
13 <span style="color:blue">LEXICON</span>
{{out}}
<pre>
1: { 1 10 11 12 13 2 3 4 5 6 7 8 9 }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">n = 13
p (1..n).sort_by(&:to_s)
</syntaxhighlight>
</lang>
{{out}}
<pre>[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]
Line 1,328 ⟶ 1,592:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn lex_sorted_vector(num: i32) -> Vec<i32> {
let (min, max) = if num >= 1 { (1, num) } else { (num, 1) };
let mut str: Vec<String> = (min..=max).map(|i| i.to_string()).collect();
Line 1,339 ⟶ 1,603:
println!("{}: {:?}", n, lex_sorted_vector(*n));
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,352 ⟶ 1,616:
=={{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)].
<langsyntaxhighlight Scalalang="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)
Line 1,359 ⟶ 1,623:
for (n <- ints) println(f"$n%3d: ${lexOrder(n).mkString("[",", ", "]")}%s")
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func lex_order (n) {
[range(1, n, n.sgn)...].sort_by { Str(_) }
}
Line 1,368 ⟶ 1,632:
[13, 21, -22].each {|n|
printf("%4s: %s\n", n, lex_order(n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,379 ⟶ 1,643:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">func lex(n: Int) -> [Int] {
return stride(from: 1, through: n, by: n.signum()).map({ String($0) }).sorted().compactMap(Int.init)
}
Line 1,385 ⟶ 1,649:
print("13: \(lex(n: 13))")
print("21: \(lex(n: 21))")
print("-22: \(lex(n: -22))")</langsyntaxhighlight>
 
{{out}}
Line 1,394 ⟶ 1,658:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc iota {num {start 0} {step 1}} {
set res {}
set end [+ $start [* $step $num]]
Line 1,403 ⟶ 1,667:
}
 
puts [lsort [iota 13 1]]</langsyntaxhighlight>
{{out}}
<pre>1 10 11 12 13 2 3 4 5 6 7 8 9</pre>
 
=={{header|VBA}}==
<langsyntaxhighlight VBlang="vb">Public Function sortlexicographically(N As Integer)
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
Line 1,423 ⟶ 1,687:
Public Sub main()
Call sortlexicographically(13)
End Sub</langsyntaxhighlight>
{{out}}
<pre>1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9, </pre>
Line 1,429 ⟶ 1,693:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<langsyntaxhighlight ecmascriptlang="wren">import "./sort" for Sort
 
var a = (1..13).map { |i| "%(i)" }.toList
Sort.quick(a)
System.print(a)</langsyntaxhighlight>
 
{{out}}
Line 1,441 ⟶ 1,705:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn lexN(n){ n.pump(List,'+(1),"toString").sort().apply("toInt") }</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach n in (T(5,13,21)){ println("%2d: %s".fmt(n,lexN(n).concat(","))) }</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits