Sort an integer array: Difference between revisions

m
mNo edit summary
m (→‎{{header|Wren}}: Minor tidy)
 
(42 intermediate revisions by 27 users not shown)
Line 1:
{{task|Sorting}}
{{Sorting Algorithm}}
Sort an array (or list) of integers in ascending numerical order.
 
;Task:
Sort an array (or list) of integers in ascending numerical order.
 
 
Use a sorting facility provided by the language/library if possible.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">nums = [2,4,3,1,2]
nums.sort()</syntaxhighlight>
You could also use the built-in sorted() function:
<syntaxhighlight lang="11l">nums = sorted([2,4,3,1,2])</syntaxhighlight>
 
=={{header|4D}}==
===English===
 
<langsyntaxhighlight lang="4d">ARRAY INTEGER($nums;0)
APPEND TO ARRAY($nums;2)
APPEND TO ARRAY($nums;4)
Line 16 ⟶ 27:
APPEND TO ARRAY($nums;2)
SORT ARRAY($nums) ` sort in ascending order
SORT ARRAY($nums;<) ` sort in descending order</langsyntaxhighlight>
 
===Français===
 
<langsyntaxhighlight lang="4d">TABLEAU ENTIER($nombres;0)
AJOUTER A TABLEAU($nombres;2)
AJOUTER A TABLEAU($nombres;4)
Line 27 ⟶ 38:
AJOUTER A TABLEAU($nombres;2)
TRIER TABLEAU($nombres) ` pour effectuer un tri par ordre croissant
TRIER TABLEAU($nombres;<) ` pour effectuer un tri par ordre décroissant</langsyntaxhighlight>
 
=={{header|8th}}==
<langsyntaxhighlight lang="forth">
[ 10,2,100 ] ' n:cmp a:sort . cr
</syntaxhighlight>
</lang>
Output is: [2,10,100]
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program integerSort64.s with selection sort */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeConstantesARM64.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessSortOk: .asciz "Table sorted.\n"
szMessSortNok: .asciz "Table not sorted !!!!!.\n"
sMessResult: .asciz "Value : @ \n"
szCarriageReturn: .asciz "\n"
.align 4
#TableNumber: .quad 1,3,6,2,5,9,10,8,4,7
TableNumber: .quad 10,9,8,7,6,5,4,3,2,1
.equ NBELEMENTS, (. - TableNumber) / 8
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrTableNumber // address number table
mov x1,0
mov x2,NBELEMENTS // number of élements
bl selectionSort
ldr x0,qAdrTableNumber // address number table
bl displayTable
ldr x0,qAdrTableNumber // address number table
mov x1,NBELEMENTS // number of élements
bl isSorted // control sort
cmp x0,1 // sorted ?
beq 1f
ldr x0,qAdrszMessSortNok // no !! error sort
bl affichageMess
b 100f
1: // yes
ldr x0,qAdrszMessSortOk
bl affichageMess
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrsZoneConv: .quad sZoneConv
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResult: .quad sMessResult
qAdrTableNumber: .quad TableNumber
qAdrszMessSortOk: .quad szMessSortOk
qAdrszMessSortNok: .quad szMessSortNok
/******************************************************************/
/* control sorted table */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the number of elements > 0 */
/* x0 return 0 if not sorted 1 if sorted */
isSorted:
stp x2,lr,[sp,-16]! // save registers
stp x3,x4,[sp,-16]! // save registers
mov x2,0
ldr x4,[x0,x2,lsl 3]
1:
add x2,x2,1
cmp x2,x1
bge 99f
ldr x3,[x0,x2, lsl 3]
cmp x3,x4
blt 98f
mov x4,x3
b 1b
98:
mov x0,0 // not sorted
b 100f
99:
mov x0,1 // sorted
100:
ldp x3,x4,[sp],16 // restaur 2 registers
ldp x2,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* selection sort */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the first element */
/* x2 contains the number of element */
selectionSort:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
mov x3,x1 // start index i
sub x7,x2,1 // compute n - 1
1: // start loop
mov x4,x3
add x5,x3,1 // init index 2
2:
ldr x1,[x0,x4,lsl 3] // load value A[mini]
ldr x6,[x0,x5,lsl 3] // load value A[j]
cmp x6,x1 // compare value
csel x4,x5,x4,lt // j -> mini
add x5,x5,1 // increment index j
cmp x5,x2 // end ?
blt 2b // no -> loop
cmp x4,x3 // mini <> j ?
beq 3f // no
ldr x1,[x0,x4,lsl 3] // yes swap A[i] A[mini]
ldr x6,[x0,x3,lsl 3]
str x1,[x0,x3,lsl 3]
str x6,[x0,x4,lsl 3]
3:
add x3,x3,1 // increment i
cmp x3,x7 // end ?
blt 1b // no -> loop
100:
ldp x6,x7,[sp],16 // restaur 2 registers
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* x0 contains the address of table */
displayTable:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x2,x0 // table address
mov x3,0
1: // loop display table
ldr x0,[x2,x3,lsl 3]
ldr x1,qAdrsZoneConv
bl conversion10 // décimal conversion
ldr x0,qAdrsMessResult
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message
add x3,x3,1
cmp x3,NBELEMENTS - 1
ble 1b
ldr x0,qAdrszCarriageReturn
bl affichageMess
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
 
</syntaxhighlight>
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
 
PROC PrintArray(INT ARRAY a INT size)
INT i
 
Put('[)
FOR i=0 TO size-1
DO
IF i>0 THEN Put(' ) FI
PrintI(a(i))
OD
Put(']) PutE()
RETURN
 
PROC Test(INT ARRAY a INT size BYTE order)
PrintE("Array before sort:")
PrintArray(a,size)
SortI(a,size,order)
PrintE("Array after sort:")
PrintArray(a,size)
PutE()
RETURN
 
PROC Main()
DEFINE ASCENDING="0"
INT ARRAY
a(10)=[1 4 65535 0 3 7 4 8 20 65530],
b(21)=[10 9 8 7 6 5 4 3 2 1 0
65535 65534 65533 65532 65531
65530 65529 65528 65527 65526],
c(8)=[101 102 103 104 105 106 107 108],
d(12)=[1 65535 1 65535 1 65535 1
65535 1 65535 1 65535]
Put(125) PutE() ;clear screen
Test(a,10,ASCENDING)
Test(b,21,ASCENDING)
Test(c,8,ASCENDING)
Test(d,12,ASCENDING)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_an_integer_array.png Screenshot from Atari 8-bit computer]
<pre>
Array before sort:
[1 4 -1 0 3 7 4 8 20 -6]
Array after sort:
[-6 -1 0 1 3 4 4 7 8 20]
 
Array before sort:
[10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]
Array after sort:
[-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10]
 
Array before sort:
[101 102 103 104 105 106 107 108]
Array after sort:
[101 102 103 104 105 106 107 108]
 
Array before sort:
[1 -1 1 -1 1 -1 1 -1 1 -1 1 -1]
Array after sort:
[-1 -1 -1 -1 -1 -1 1 1 1 1 1 1]
</pre>
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">//Comparison function must returns Numbers even though it deals with integers.
function compare(x:int, y:int):Number
{
Line 41 ⟶ 287:
}
var nums:Vector.<int> = Vector.<int>([5,12,3,612,31,523,1,234,2]);
nums.sort(compare);</langsyntaxhighlight>
 
=={{header|Ada}}==
{{works with|GNAT|GPL 2006}}
<langsyntaxhighlight lang="ada">with Gnat.Heap_Sort_G;
procedure Integer_Sort is
Line 87 ⟶ 333:
begin
Sort(Values);
end Integer_Sort;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 95 ⟶ 341:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<langsyntaxhighlight lang="algol68">CO PR READ "shell_sort.a68" PR CO
MODE TYPE = INT;
 
Line 118 ⟶ 364:
in place shell sort(LOC[LWB seq: UPB seq]TYPE:=seq);
 
print((shell sort((2, 4, 3, 1, 2)), new line))</langsyntaxhighlight>
Output:
<pre>
Line 126 ⟶ 372:
=={{header|ALGOL W}}==
Algol W doesn't have standard sorting facilities. This uses the Algol W quicksort sample in the Sorting Algorithms Quicksort task.
<langsyntaxhighlight lang="algolw">begin
% use the quicksort procedure from the Sorting_Algorithms/Quicksort task %
% Quicksorts in-place the array of integers v, from lb to ub - external %
Line 141 ⟶ 387:
for i := 1 until 5 do writeon( i_w := 1, s_w := 1, t( i ) )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 149 ⟶ 395:
=={{header|APL}}==
{{works with|APL2}}
<langsyntaxhighlight lang="apl"> X←63 92 51 92 39 15 43 89 36 69
X[⍋X]
15 36 39 43 51 63 69 89 92 92</langsyntaxhighlight>
 
 
=={{header|AppleScript}}==
Line 164 ⟶ 409:
for which AppleScript was presumably designed.
 
<langsyntaxhighlight AppleScriptlang="applescript">use framework "Foundation"
 
-- sort :: [a] -> [a]
Line 206 ⟶ 451:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<pre>{{0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9},
{"alpha", "beta", "delta", "epsilon", "eta", "gamma",
"iota", "kappa", "lambda", "mu", "theta", "zeta"}}</pre>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
 
/* ARM assembly Raspberry PI */
/* program integerSort.s with selection sort */
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessSortOk: .asciz "Table sorted.\n"
szMessSortNok: .asciz "Table not sorted !!!!!.\n"
sMessResult: .asciz "Value : @ \n"
szCarriageReturn: .asciz "\n"
.align 4
TableNumber: .int 1,3,6,2,5,9,10,8,4,7
#TableNumber: .int 10,9,8,7,6,5,4,3,2,1
.equ NBELEMENTS, (. - TableNumber) / 4
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
1:
ldr r0,iAdrTableNumber @ address number table
mov r1,#0
mov r2,#NBELEMENTS @ number of élements
bl selectionSort
ldr r0,iAdrTableNumber @ address number table
bl displayTable
ldr r0,iAdrTableNumber @ address number table
mov r1,#NBELEMENTS @ number of élements
bl isSorted @ control sort
cmp r0,#1 @ sorted ?
beq 2f
ldr r0,iAdrszMessSortNok @ no !! error sort
bl affichageMess
b 100f
2: @ yes
ldr r0,iAdrszMessSortOk
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsMessResult: .int sMessResult
iAdrTableNumber: .int TableNumber
iAdrszMessSortOk: .int szMessSortOk
iAdrszMessSortNok: .int szMessSortNok
/******************************************************************/
/* control sorted table */
/******************************************************************/
/* r0 contains the address of table */
/* r1 contains the number of elements > 0 */
/* r0 return 0 if not sorted 1 if sorted */
isSorted:
push {r2-r4,lr} @ save registers
mov r2,#0
ldr r4,[r0,r2,lsl #2]
1:
add r2,#1
cmp r2,r1
movge r0,#1
bge 100f
ldr r3,[r0,r2, lsl #2]
cmp r3,r4
movlt r0,#0
blt 100f
mov r4,r3
b 1b
100:
pop {r2-r4,lr}
bx lr @ return
/******************************************************************/
/* selection sort */
/******************************************************************/
/* r0 contains the address of table */
/* r1 contains the first element */
/* r2 contains the number of element */
selectionSort:
push {r1-r7,lr} @ save registers
mov r3,r1 @ start index i
sub r7,r2,#1 @ compute n - 1
1: @ start loop
mov r4,r3
add r5,r3,#1 @ init index 2
2:
ldr r1,[r0,r4,lsl #2] @ load value A[mini]
ldr r6,[r0,r5,lsl #2] @ load value A[j]
cmp r6,r1 @ compare value
movlt r4,r5 @ j -> mini
add r5,#1 @ increment index j
cmp r5,r2 @ end ?
blt 2b @ no -> loop
cmp r4,r3 @ mini <> j ?
beq 3f @ no
ldr r1,[r0,r4,lsl #2] @ yes swap A[i] A[mini]
ldr r6,[r0,r3,lsl #2]
str r1,[r0,r3,lsl #2]
str r6,[r0,r4,lsl #2]
3:
add r3,#1 @ increment i
cmp r3,r7 @ end ?
blt 1b @ no -> loop
100:
pop {r1-r7,lr}
bx lr @ return
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* r0 contains the address of table */
displayTable:
push {r0-r3,lr} @ save registers
mov r2,r0 @ table address
mov r3,#0
1: @ loop display table
ldr r0,[r2,r3,lsl #2]
ldr r1,iAdrsZoneConv @
bl conversion10 @ décimal conversion
ldr r0,iAdrsMessResult
ldr r1,iAdrsZoneConv @ insert conversion
bl strInsertAtCharInc
bl affichageMess @ display message
add r3,#1
cmp r3,#NBELEMENTS - 1
ble 1b
ldr r0,iAdrszCarriageReturn
bl affichageMess
100:
pop {r0-r3,lr}
bx lr
iAdrsZoneConv: .int sZoneConv
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">arr: [2 3 5 8 4 1 6 9 7]
sort 'arr ; in-place
loop arr => print</syntaxhighlight>
 
{{out}}
 
<pre>1
2
3
4
5
6
7
8
9</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">numbers = 5 4 1 2 3
sort, numbers, N D%A_Space%
Msgbox % numbers</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SORT_AN_INTEGER_ARRAY.AWK
BEGIN {
Line 235 ⟶ 658:
printf("\t%s\n",description)
}
</syntaxhighlight>
</lang>
<p>output:</p>
<pre>
Line 246 ⟶ 669:
There is no ascending sort function in Axe, but there is a descending sort function. One can either implement a custom ascending sorting function or simply reverse the output from SortD.
 
<langsyntaxhighlight lang="axe">2→{L₁}
4→{L₁+1}
3→{L₁+2}
Line 252 ⟶ 675:
2→{L₁+4}
 
SortD(L₁,5)</langsyntaxhighlight>
 
=={{header|Babel}}==
Line 258 ⟶ 681:
Use the sortval operator to sort an array of integers (val-array in Babel terminology). The following code creates a list of random values, converts it to a val-array, sorts that val-array, then converts it back to a list for display using the lsnum utility.
 
<langsyntaxhighlight lang="babel">babel> nil { zap {1 randlf 100 rem} 20 times collect ! } nest dup lsnum ! --> Create a list of random numbers
( 20 47 69 71 18 10 92 9 56 68 71 92 45 92 12 7 59 55 54 24 )
babel> ls2lf --> Convert list to array for sorting
babel> dup {fnord} merge_sort --> The internal sort operator
babel> ar2ls lsnum ! --> Display the results
( 7 9 10 12 18 20 24 45 47 54 55 56 59 68 69 71 71 92 92 92 )</langsyntaxhighlight>
 
In Babel, lists and arrays are distinct. If you want to sort a list, use the lssort utility:
 
<langsyntaxhighlight lang="babel">babel> ( 68 73 63 83 54 67 46 53 88 86 49 75 89 83 28 9 34 21 20 90 )
babel> {lt?} lssort ! lsnum !
( 9 20 21 28 34 46 49 53 54 63 67 68 73 75 83 83 86 88 89 90 )</langsyntaxhighlight>
 
To reverse the sort-order, use the 'gt?' predicate instead of the 'lt?' predicate:
 
<langsyntaxhighlight lang="babel">babel> ( 68 73 63 83 54 67 46 53 88 86 49 75 89 83 28 9 34 21 20 90 ) {gt?} lssort ! lsnum !
( 90 89 88 86 83 83 75 73 68 67 63 54 53 49 46 34 28 21 20 9 )</langsyntaxhighlight>
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="freebasic">' Sort an integer array
DECLARE values[5] TYPE NUMBER
values[0] = 23
Line 290 ⟶ 713:
PRINT values[i], ", ";
NEXT
PRINT values[4]</langsyntaxhighlight>
 
{{out}}
Line 300 ⟶ 723:
{{works with|BBC BASIC for Windows}}
Uses the supplied SORTLIB library.
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB"
sort% = FN_sortinit(0,0)
Line 312 ⟶ 735:
PRINT ; array(i%) ", ";
NEXT
PRINT ; array(i%)</langsyntaxhighlight>
Output:
<pre>
1, 2, 3, 4, 5, 6, 7, 8, 9
</pre>
 
=={{header|Beads}}==
<syntaxhighlight lang="beads">beads 1 program 'Sort an integer array'
calc main_init
var arr = [4, 1, 2, -1, 3, 0, 2]
var newarr : array of num
loop across:arr sort:val count:c val:v
newarr[c] = v</syntaxhighlight>
 
=={{header|Befunge}}==
{{works with|befungee}}
Elements of the array are read from standard input, preceded by their quantity. The algorithm uses counting sort and allows numbers between 1 and 60, inclusive.
<syntaxhighlight lang="befunge">v
<lang Befunge>v
> 543** > :#v_ $&> :#v_ 1 > :0g > :#v_ $ 1+: 543** `! #v_ 25*,@
^-1p0\0:< ^-1 p0\+1 g0:&< ^-1\.:\<
^ <</langsyntaxhighlight>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">∧ [4, 1, 2, ¯9, ¯5, 3, 6, 9, ¯2]</syntaxhighlight>
{{out}}
<pre>⟨ ¯9 ¯5 ¯2 1 2 3 4 6 9 ⟩</pre>
 
=={{header|Bracmat}}==
Line 338 ⟶ 774:
{!} 21</pre>
To complete the task need to unfold the terms with a numerical factor >1:
<langsyntaxhighlight lang="bracmat">{sort takes a list of space-separated integers}
(sort=
sum elem sorted n
Line 354 ⟶ 790:
& !sorted);
out$sort$(9 -2 1 2 8 0 1 2);</langsyntaxhighlight>
{{out}}
Output:
<pre>-2 0 1 1 2 2 8 9</pre>
This solution becomes very ineffective for long lists. To add a single term to an already sorted sum of N terms requires on average N/2 steps. It is much more efficient to merge two already sorted sums of about equal length.
Line 361 ⟶ 797:
 
=={{header|Burlesque}}==
<langsyntaxhighlight lang="burlesque">{1 3 2 5 4}><</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdlib.h> /* qsort() */
#include <stdio.h> /* printf() */
 
Line 380 ⟶ 816:
nums[0], nums[1], nums[2], nums[3], nums[4]);
return 0;
}</langsyntaxhighlight>
 
''Caution:'' An older version of <tt>intcmp()</tt> did <tt>return *a - *b</tt>. This is only correct when the subtraction does not overflow. Suppose that <tt>*a = 2000000000</tt> and <tt>*b = -2000000000</tt> on a machine with 32-bit <tt>int</tt>. The subtraction <tt>*a - *b</tt> would overflow to <tt>-294967296</tt>, and <tt>intcmp()</tt> would believe <tt>*a < *b</tt>, but the correct answer is <tt>*a > *b</tt>.
 
=={{header|C sharp|C#}}==
 
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
public class Program {
static void Main() {
int[] unsorted = { 6, 2, 7, 8, 3, 1, 10, 5, 4, 9 };
Array.Sort(unsorted);
}
}</syntaxhighlight>
 
=={{header|C++}}==
Line 388 ⟶ 836:
 
===Simple Array===
<langsyntaxhighlight lang="cpp">#include <algorithm>
 
int main()
Line 395 ⟶ 843:
std::sort(nums, nums+sizeof(nums)/sizeof(int));
return 0;
}</langsyntaxhighlight>
 
===<tt>std::vector</tt>===
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <vector>
 
Line 411 ⟶ 859:
std::sort(nums.begin(), nums.end());
return 0;
}</langsyntaxhighlight>
 
===<tt>std::list</tt>===
<langsyntaxhighlight lang="cpp">#include <list>
 
int main()
Line 426 ⟶ 874:
nums.sort();
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
 
<lang csharp>using System;
using System.Collections.Generic;
 
public class Program {
static void Main() {
int[] unsorted = { 6, 2, 7, 8, 3, 1, 10, 5, 4, 9 };
Array.Sort(unsorted);
}
}</lang>
 
=={{header|Clean}}==
We use list and array comprehensions to convert an array to and from a list in order to use the built-in <tt>sort</tt> on lists.
<langsyntaxhighlight lang="clean">import StdEnv
 
sortArray :: (a e) -> a e | Array a e & Ord e
Line 448 ⟶ 884:
 
Start :: {#Int}
Start = sortArray {2, 4, 3, 1, 2}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(sort [5 4 3 2 1]) ; sort can also take a comparator function
(1 2 3 4 5)</langsyntaxhighlight>
 
=={{header|COBOL}}==
{{works with|Visual COBOL}}
<langsyntaxhighlight lang="cobol"> PROGRAM-ID. sort-ints.
DATA DIVISION.
Line 477 ⟶ 913:
END-PERFORM
DISPLAY SPACE
.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
In Common Lisp, the ''sort'' function takes a predicate that is used as the comparator. This parameter can be any two-argument function. To sort a sequence (list or array) of integers, call ''sort'' with the < operator as the predicate:
<langsyntaxhighlight lang="lisp">CL-USER> (sort #(9 -2 1 2 8 0 1 2) #'<)
#(-2 0 1 1 2 2 8 9)</langsyntaxhighlight>
 
=={{header|Crystal}}==
Example demonstrating the support for copy sort and in-place sort (like Ruby)
<syntaxhighlight lang="ruby">
<lang Ruby>
a = [5, 4, 3, 2, 1]
puts a.sort
Line 497 ⟶ 933:
puts a
# => [1, 2, 3, 4, 5]
</syntaxhighlight>
</lang>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm;
 
void main() {
Line 506 ⟶ 942:
data.sort(); // in-place
assert(data == [1, 2, 2, 3, 4]);
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">uses Types, Generics.Collections;
 
var
Line 516 ⟶ 952:
a := TIntegerDynArray.Create(5, 4, 3, 2, 1);
TArray.Sort<Integer>(a);
end;</langsyntaxhighlight>
=={{header|Déjà Vu}}==
<lang dejavu>!. sort [ 5 4 3 2 1 ]</lang>
{{out}}
<pre>[ 1 2 3 4 5 ]</pre>
 
=={{header|DWScript}}==
<langsyntaxhighlight Delphilang="delphi">var a : array of Integer := [5, 4, 3, 2, 1];
a.Sort; // ascending natural sort
PrintLn(a.Map(IntToStr).Join(',')); // 1,2,3,4,5</langsyntaxhighlight>
 
=={{header|E}}==
<lang e>[2,4,3,1,2].sort()</lang>
=={{header|Elena}}==
ELENA 4.1 :
<lang elena>import system'routines;
import extensions;
public program()
{
var unsorted := new int[]::(6, 2, 7, 8, 3, 1, 10, 5, 4, 9);
console.printLine(unsorted.clone().sort(ifOrdered).asEnumerable())
}</lang>
 
=={{header|Elixir}}==
<lang elixir>list = [2, 4, 3, 1, 2]
IO.inspect Enum.sort(list)
IO.inspect Enum.sort(list, &(&1>&2))</lang>
 
=={{header|Déjà Vu}}==
<syntaxhighlight lang="dejavu">!. sort [ 5 4 3 2 1 ]</syntaxhighlight>
{{out}}
<pre>[ 1 2 3 4 5 ]</pre>
[1, 2, 2, 3, 4]
[4, 3, 2, 2, 1]
</pre>
 
=={{header|ErlangE}}==
<syntaxhighlight lang erlang="e">List = [2, 4, 3, 1, 2].sort()</syntaxhighlight>
SortedList = lists:sort(List).</lang>
 
=={{header|Euphoria}}==
<lang euphoria>include sort.e
print(1,sort({20, 7, 65, 10, 3, 0, 8, -60}))</lang>
 
=={{header|EGL}}==
{{works with|EDT}}
The following works in EDT with Rich UI and stand-alone programs.
<langsyntaxhighlight EGLlang="egl">program SortExample
 
function main()
Line 578 ⟶ 985:
end
end</langsyntaxhighlight>
{{works with|RBD}}
The following works in RBD but only with Rich UI programs.
<langsyntaxhighlight EGLlang="egl">test1 int[] = [1,-1,8,-8,2,-2,7,-7,3,-3,6,-6,9,-9,4,-4,5,-5,0];
RUILib.sort(test1, sortFunction);
 
Line 587 ⟶ 994:
function sortFunction(a any in, b any in) returns (int)
return ((a as int) - (b as int));
end</langsyntaxhighlight>
 
=={{header|Eiffel}}==
 
[https://github.com/ljr1981/rosettacode_answers/blob/main/testing/rc_array_sort/rc_array_sort_test_set.e Full example code]
 
Using a SORTED_TWO_WAY_LIST means that the contents of the list will be sorted ascending automatically.
The list can be easily sorted in reverse. There is a call for `sort' to manually initiate sorting.
 
<syntaxhighlight lang="eiffel">
local
l_array: SORTED_TWO_WAY_LIST [INTEGER]
do
create l_array.make_from_iterable (<<9,8,7,6,5,4,3,2,1,0>>)
end
</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 5.0 :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
public program()
{
var unsorted := new int[]{6, 2, 7, 8, 3, 1, 10, 5, 4, 9};
console.printLine(unsorted.clone().sort(ifOrdered).asEnumerable())
}</syntaxhighlight>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">list = [2, 4, 3, 1, 2]
IO.inspect Enum.sort(list)
IO.inspect Enum.sort(list, &(&1>&2))</syntaxhighlight>
 
{{out}}
<pre>
[1, 2, 2, 3, 4]
[4, 3, 2, 2, 1]
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">List = [2, 4, 3, 1, 2].
SortedList = lists:sort(List).</syntaxhighlight>
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">include sort.e
print(1,sort({20, 7, 65, 10, 3, 0, 8, -60}))</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">// sorting an array in place
let nums = [| 2; 4; 3; 1; 2 |]
Array.sortInPlace nums
 
// create a sorted copy of a list
let nums2 = [2; 4; 3; 1; 2]
let sorted = List.sort nums2</syntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">{ 1 4 9 2 3 0 5 } natural-sort .</langsyntaxhighlight>
 
=={{header|Fantom}}==
Line 608 ⟶ 1,070:
{{works with|Win32Forth|4.2}}
===Win32Forth===
<langsyntaxhighlight lang="forth">create test-data 2 , 4 , 3 , 1 , 2 ,
test-data 5 cell-sort
</syntaxhighlight>
</lang>
===ANS/ISO Forth===
{{works with|GForth}}
Line 616 ⟶ 1,078:
 
Standard Forth does not have a library sort
<langsyntaxhighlight lang="forth">100000 CONSTANT SIZE
 
CREATE MYARRAY SIZE CELLS ALLOT
Line 658 ⟶ 1,120:
DUP 2 < IF 2DROP EXIT THEN 1- CELLS OVER + QSORT ;</LANG>
Test at the console
<langsyntaxhighlight lang="forth">FILLIT ok
MYARRAY SIZE QUICKSORT ok</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Silverfrost FTN95}}
<langsyntaxhighlight lang="fortran">CALL ISORT@(b, a, n)
! n = number of elements
! a = array to be sorted
! b = array of indices of a. b(1) 'points' to the minimum value etc.</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
Qsort is not buildin, but include in the compiler package.
<langsyntaxhighlight lang="freebasic">' version 11-03-2016
' compile with: fbc -s console
 
Line 727 ⟶ 1,189:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>unsorted array
Line 737 ⟶ 1,199:
=={{header|Frink}}==
The following sorts an array in-place.
<langsyntaxhighlight lang="frink">a = [5, 2, 4, 1, 6, 7, 9, 3, 8, 0]
sort[a]</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<lang fsharp>// sorting an array in place
let nums = [| 2; 4; 3; 1; 2 |]
Array.sortInPlace nums
 
// create a sorted copy of a list
let nums2 = [2; 4; 3; 1; 2]
let sorted = List.sort nums2</lang>
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">nums = [5, 2, 78, 2, 578, -42]
println( sort(nums) ) // sort in ascending order
println( nums.sortWith((>)) ) // sort in descending order</langsyntaxhighlight>
 
{{out}}
Line 761 ⟶ 1,214:
</pre>
 
=={{header|GAPFutureBasic}}==
<syntaxhighlight lang="futurebasic">
<lang gap>a := [ 8, 2, 5, 9, 1, 3, 6, 7, 4 ];
window 1, @"Sort an integer array"
# Make a copy (with "b := a;", b and a would point to the same list)
b := ShallowCopy(a);
 
void local fn DoIt
# Sort in place
CFArrayRef array = @[@13,@71,@42,@8,@5,@27]
Sort(a);
array = fn ArraySortedArrayUsingSelector( array, @"compare:" )
a;
print fn ArrayComponentsJoinedByString( array, @", " )
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
end fn
 
fn DoIt
# Sort without changing the argument
 
SortedList(b);
HandleEvents
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
</syntaxhighlight>
b;
{{out}}
# [ 8, 2, 5, 9, 1, 3, 6, 7, 4 ]</lang>
<pre>
5, 8, 13, 27, 42, 71
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sort_an_integer_array}}
 
'''Solution'''
 
[[File:Fōrmulæ - Sort an integer array 01.png]]
 
[[File:Fōrmulæ - Sort an integer array 02.png]]
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=1f1d244aa95c329eb87cb538f0d5fc4a Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim iArray As Integer[] = [8, 2, 5, 9, 1, 3, 6, 7, 4]
Dim iTemp As Integer
Line 790 ⟶ 1,256:
Print Left(sOutput, -2)
 
End</langsyntaxhighlight>
 
Output:
Line 796 ⟶ 1,262:
1, 2, 3, 4, 5, 6, 7, 8, 9
</pre>
 
=={{header|GAP}}==
<syntaxhighlight lang="gap">a := [ 8, 2, 5, 9, 1, 3, 6, 7, 4 ];
# Make a copy (with "b := a;", b and a would point to the same list)
b := ShallowCopy(a);
 
# Sort in place
Sort(a);
a;
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
 
# Sort without changing the argument
SortedList(b);
# [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
b;
# [ 8, 2, 5, 9, 1, 3, 6, 7, 4 ]</syntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
import "fmt"
import "sort"
Line 806 ⟶ 1,288:
sort.Ints(nums)
fmt.Println(nums)
}</langsyntaxhighlight>
 
=={{header|Golfscript}}==
<langsyntaxhighlight lang="golfscript">[2 4 3 1 2]$</langsyntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">println ([2,4,0,3,1,2,-12].sort())</langsyntaxhighlight>
 
Output:
Line 820 ⟶ 1,302:
{{works with|GHC|GHCi|6.6}}
<langsyntaxhighlight lang="haskell">nums = [2,4,3,1,2] :: [Int]
sorted = List.sort nums</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">DIMENSION array(100)
 
array = INT( RAN(100) )
SORT(Vector=array, Sorted=array) </langsyntaxhighlight>
 
=={{header|Huginn}}==
<langsyntaxhighlight lang="huginn">main() {
nums = [2, 4, 3, 1, 2];
nums.sort();
}</langsyntaxhighlight>
 
=={{header|IDL}}==
<lang idl>result = array[sort(array)]</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 842 ⟶ 1,321:
 
In the example below, L will remain an unsorted list and S will be sorted.
<langsyntaxhighlight Iconlang="icon">S := sort(L:= [63, 92, 51, 92, 39, 15, 43, 89, 36, 69]) # will sort a list</langsyntaxhighlight>
 
=={{header|IDL}}==
<syntaxhighlight lang="idl">result = array[sort(array)]</syntaxhighlight>
 
=={{header|Inform 7}}==
<langsyntaxhighlight lang="inform7">let L be {5, 4, 7, 1, 18};
sort L;</langsyntaxhighlight>
 
=={{header|Io}}==
<langsyntaxhighlight lang="lua">mums := list(2,4,3,1,2)
sorted := nums sort # returns a new sorted array. 'nums' is unchanged
nums sortInPlace # sort 'nums' "in-place"</langsyntaxhighlight>
 
=={{header|J}}==
<syntaxhighlight lang ="j">/:~</langsyntaxhighlight>
The verb<tt> /:~ </tt>sorts <i>anything</i> that J can represent. For example:
 
<langsyntaxhighlight lang="j"> ] a=: 10 ?@$ 100 NB. random vector
63 92 51 92 39 15 43 89 36 69
/:~ a
15 36 39 43 51 63 69 89 92 92</langsyntaxhighlight>
Arrays of any rank are treated as lists of component arrays. Thus <tt>/:~</tt> sorts not only atoms within a list, but whole lists within a table, tables within a three-axis array, and so on. The level of structure at which sorting occurs may also be specified, so that <tt>/:~"1</tt> sorts the atoms within the finest-grained list within the array, regardless of the overall rank of the array. See the [https[j://code.jsoftware.com/wiki/Essays/The_TAO_of_J |Total Array Ordering essay]] on the JWiki for more details.
 
This code also applies to any data type.
Line 867 ⟶ 1,349:
=={{header|Java}}==
===Array===
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class Example {
Line 875 ⟶ 1,357:
Arrays.sort(nums);
}
}</langsyntaxhighlight>
 
===List===
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Line 889 ⟶ 1,371:
Collections.sort(nums);
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 896 ⟶ 1,378:
JavaScript sorts lexically by default, so "10000" comes before "2". To sort numerically, a custom comparator is used.
 
<langsyntaxhighlight lang="javascript">function int_arr(a, b) {
return a - b;
}
var numbers = [20, 7, 65, 10, 3, 0, 8, -60];
numbers.sort(int_arr);
document.write(numbers);</langsyntaxhighlight>
 
=={{header|KotlinJinja}}==
<syntaxhighlight lang="jinja">
<lang scala>// version 1.0.6
from jinja2 import Template
 
print(Template("{{ [53, 17, 42, 61, 35] | sort }}").render())
fun main(args: Array<String>) {
</syntaxhighlight>
val ints = intArrayOf(6, 2, 7, 8, 3, 1, 10, 5, 4, 9)
Descending order:
ints.sort()
<syntaxhighlight lang="jinja">
println(ints.joinToString(prefix = "[", postfix = "]"))
from jinja2 import Template
}</lang>
print(Template("{{ [53, 17, 42, 61, 35] | sort(reverse=true) }}").render())
 
</syntaxhighlight>
{{out}}
<pre>
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
</pre>
 
=={{header|Lasso}}==
<lang Lasso>local(array) = array(5,20,3,2,6,1,4)
#array->sort
#array // 1, 2, 3, 4, 5, 6, 20
 
// Reverse the sort order
#array->sort(false)
#array // 20, 6, 5, 4, 3, 2, 1</lang>
 
=={{header|jq}}==
jq's builtin <code>sort</code> filter sorts the elements of an array in ascending order:
<langsyntaxhighlight lang="jq">[2,1,3] | sort # => [1,2,3]</langsyntaxhighlight>
 
=={{header|Julia}}==
Julia has both out-of-place (<code>sort</code>) and in-place (<code>sort!</code>) sorting functions in its standard-library:
<langsyntaxhighlight lang="julia">julia> a = [4,2,3,1]
4-element Int32 Array:
4
Line 964 ⟶ 1,434:
2
3
4</langsyntaxhighlight>
 
=={{header|K}}==
<langsyntaxhighlight lang="k"> num: -10?10 / Integers from 0 to 9 in random order
5 9 4 2 0 3 6 1 8 7
 
srt: {x@<x} / Generalized sort ascending
srt num
0 1 2 3 4 5 6 7 8 9</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.0.6
 
fun main(args: Array<String>) {
val ints = intArrayOf(6, 2, 7, 8, 3, 1, 10, 5, 4, 9)
ints.sort()
println(ints.joinToString(prefix = "[", postfix = "]"))
}</syntaxhighlight>
 
{{out}}
<pre>
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
1) sorting digits in a number returns a new number of ordered digits
{W.sort < 51324}
-> 12345
 
2) sorting a sequence of numbers returns a new ordered sequence of these numbers
{S.sort < 51 111 33 2 41}
-> 2 33 41 51 111
 
3) sorting an array of numbers returns the same array ordered
{A.sort! < {A.new 51 111 33 2 41}}
-> [2,33,41,51,111]
</syntaxhighlight>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">local(array) = array(5,20,3,2,6,1,4)
#array->sort
#array // 1, 2, 3, 4, 5, 6, 20
 
// Reverse the sort order
#array->sort(false)
#array // 20, 6, 5, 4, 3, 2, 1</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
LB has an array-sort command. Parameters are arrayname, start term, finish term.
<langsyntaxhighlight lang="lb">N =20
dim IntArray( N)
 
Line 991 ⟶ 1,499:
for i =1 to N
print IntArray( i)
next i</langsyntaxhighlight>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">l = [7, 4, 23]
l.sort()
put l
-- [4, 7, 23]</langsyntaxhighlight>
 
=={{header|LiveCode}}==
LiveCode can sort lines or items natively. The delimiter for items can be set to any single character, but defaults to comma.
<langsyntaxhighlight LiveCodelang="livecode">put "3,2,5,4,1" into X
sort items of X numeric
put X
-- outputs "1,2,3,4,5"</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">t = {4, 5, 2}
table.sort(t)
print(unpack(t))</langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">sort([5,7,8,3,6,1]);
sort(Array([5,7,8,3,6,1]))</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang ="mathematica">numbers = Sort[{2,4,3,1,2}]</langsyntaxhighlight>
 
=={{header|MATLAB}}==
<langsyntaxhighlight Matlablang="matlab">a = [4,3,7,-2,9,1]; b = sort(a) % b contains elements of a in ascending order
[b,idx] = sort(a) % b contains a(idx)</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">sort([9, 4, 3, 7, 6, 1, 10, 2, 8, 5]);</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">arr = #(5, 4, 3, 2, 1)
arr = sort arr</langsyntaxhighlight>
 
=={{header|Mercury}}==
<syntaxhighlight lang="text">:- module sort_int_list.
:- interface.
:- import_module io.
Line 1,043 ⟶ 1,551:
list.sort(Nums, Sorted),
io.write(Sorted, !IO),
io.nl(!IO).</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">(5 2 1 3 4) '> sort print</langsyntaxhighlight>
{{out}}
<pre>
Line 1,055 ⟶ 1,563:
=={{header|Modula-3}}==
Modula-3 provides a generic <tt>ArraySort</tt> module, as well as an instance of that module for integers called <tt>IntArraySort</tt>.
<langsyntaxhighlight lang="modula3">MODULE ArraySort EXPORTS Main;
 
IMPORT IntArraySort;
Line 1,063 ⟶ 1,571:
BEGIN
IntArraySort.Sort(arr);
END ArraySort.</langsyntaxhighlight>
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">SORTARRAY(X,SEP)
;X is the list of items to sort
;X1 is the temporary array
Line 1,077 ⟶ 1,585:
SET I="" FOR SET I=$O(X1(I)) Q:I="" SET Y=$SELECT($L(Y)=0:I,1:Y_SEP_I)
KILL I,X1
QUIT Y</langsyntaxhighlight>
Output:<pre>USER>W $$SORTARRAY^ROSETTA("3,5,1,99,27,16,0,-1",",")
-1,0,1,3,5,16,27,99
Line 1,084 ⟶ 1,592:
=={{header|Nanoquery}}==
'sort' in the Nanoquery standard library has a Quicksort function.
<langsyntaxhighlight Nanoquerylang="nanoquery">% import sort
% println sort({2,4,3,1,2})
[1, 2, 2, 3, 4]</langsyntaxhighlight>
 
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
<doc><h2>Sort integer array, in Neko</h2>
<p>Array sort function modified from Haxe codegen with -D neko-source</p>
Line 1,128 ⟶ 1,636:
 
/* Also returns the sorted array for chaining */
$print(sort($array(3,1,4,1,5,9,2,6,5,3,5,8)), "\n")</langsyntaxhighlight>
 
{{out}}
Line 1,138 ⟶ 1,646:
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">using System.Console;
 
module IntSort
Line 1,150 ⟶ 1,658:
WriteLine(sorted);
}
}</langsyntaxhighlight>
Output:
<pre>[1, 5, 3, 7, 2, 8, 3, 9]
Line 1,156 ⟶ 1,664:
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
Line 1,177 ⟶ 1,685:
say sorted.strip('t')
 
return</langsyntaxhighlight>
 
'''Output'''
Line 1,187 ⟶ 1,695:
NetRexx reimplementations of the [[#REXX|Rexx]] samples from below:
 
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols
 
Line 1,258 ⟶ 1,766:
say
 
return</langsyntaxhighlight>
 
'''Output'''
Line 1,309 ⟶ 1,817:
</pre>
 
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols
 
Line 1,362 ⟶ 1,870:
end
 
return</langsyntaxhighlight>
 
'''Output'''
Line 1,371 ⟶ 1,879:
 
=={{header|Nial}}==
<langsyntaxhighlight lang="nial">sort >= 9 6 8 7 1 10
= 10 9 8 7 6 1</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import algorithm
var a: array[0..8, int] = [2, 3, 5, 8, 4, 1, 6, 9, 7]
a.sort(system.cmp[int], Ascending)
for x in a:
echo( x)</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,394 ⟶ 1,902:
=={{header|Niue}}==
'''Library'''
<langsyntaxhighlight Niuelang="niue">2 6 1 0 3 8 sort .s
0 1 2 3 6 8</langsyntaxhighlight>
 
=={{header|Objective-C}}==
<lang objc>NSArray *nums = @[@2, @4, @3, @1, @2];
NSArray *sorted = [nums sortedArrayUsingSelector:@selector(compare:)];</lang>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">bundle Default {
class Sort {
function : Main(args : System.String[]) ~ Nil {
Line 1,409 ⟶ 1,913:
}
}
}</langsyntaxhighlight>
 
=={{header|Objective-C}}==
<syntaxhighlight lang="objc">NSArray *nums = @[@2, @4, @3, @1, @2];
NSArray *sorted = [nums sortedArrayUsingSelector:@selector(compare:)];</syntaxhighlight>
 
=={{header|OCaml}}==
===Array===
<langsyntaxhighlight lang="ocaml">let nums = [|2; 4; 3; 1; 2|]
Array.sort compare nums</langsyntaxhighlight>
 
===List===
<langsyntaxhighlight lang="ocaml">let nums = [2; 4; 3; 1; 2]
let sorted = List.sort compare nums</langsyntaxhighlight>
 
=={{header|Octave}}==
Line 1,424 ⟶ 1,932:
The variable <tt>v</tt> can be a vector or a matrix (columns will be sorted).
 
<langsyntaxhighlight lang="octave">sortedv = sort(v);</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">[ 8, 2, 5, 9, 1, 3, 6, 7, 4 ] sort</langsyntaxhighlight>
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="rexx">a = .array~of(4, 1, 6, -2, 99, -12)
say "The sorted numbers are"
say a~sortWith(.numericComparator~new)~makeString</langsyntaxhighlight>
Output:
<pre>
Line 1,447 ⟶ 1,955:
=={{header|Order}}==
Passing the less-than operator to the built-in sequence (i.e. list) sort function:
<langsyntaxhighlight lang="c">#include <order/interpreter.h>
 
ORDER_PP( 8seq_sort(8less, 8seq(2, 4, 3, 1, 2)) )</langsyntaxhighlight>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
Nums = [2 4 3 1 2]
Sorted = {List.sort Nums Value.'<'}
in
{Show Sorted}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang ="parigp">vecsort(v)</langsyntaxhighlight>
 
=={{header|Peloton}}==
Sorting a list of numbers as strings and as numbers (from the manual.)
<langsyntaxhighlight lang="sgml">Construct a list of numbers
<@ LETCNSLSTLIT>L|65^84^1^25^77^4^47^2^42^44^41^25^69^3^51^45^4^39^</@>
Numbers sort as strings
Line 1,477 ⟶ 1,985:
<@ SAYDMPLST>list</@>
<@ ACTSRTENTLSTLIT>list|__NumericDescending</@>
<@ SAYDMPLST>list</@></langsyntaxhighlight>
 
Output
<langsyntaxhighlight lang="html">Construct a list of numbers
Numbers sort as strings
Line 1,494 ⟶ 2,002:
1^2^3^4^4^25^25^39^41^42^44^45^47^51^65^69^77^84^
84^77^69^65^51^47^45^44^42^41^39^25^25^4^4^3^2^1^</langsyntaxhighlight>
 
=={{header|Perl}}==
{{works with|Perl|5.8.6}}
<langsyntaxhighlight lang="perl">@nums = (2,4,3,1,2);
@sorted = sort {$a <=> $b} @nums;</langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
{{libheader|Phix/basics}}
If <code>@a</code> contains only numbers:
<!--<syntaxhighlight lang="phix">-->
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
 
=={{header|Phixmonti}}==
<lang perl6>my @sorted = sort @a;</lang>
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt
 
( 9 10 3 1 4 5 8 7 6 2 ) sort print</syntaxhighlight>
For an in-place sort:
 
<lang perl6>@a .= sort;</lang>
 
=={{header|Phix}}==
<lang Phix>?sort({9, 10, 3, 1, 4, 5, 8, 7, 6, 2})</lang>
 
=={{header|PHP}}==
{{works with|PHP|4.4.4 CLI}}
<langsyntaxhighlight lang="php"><?php
$nums = array(2,4,3,1,2);
sort($nums);
?></langsyntaxhighlight>
 
=={{header|PicoLisp}}==
The [http://software-lab.de/doc/refS.html#sort sort] function in PicoLisp
returns already by default an ascending list (of any type, not only integers):
<langsyntaxhighlight PicoLisplang="picolisp">(sort (2 4 3 1 2))
-> (1 2 2 3 4)</langsyntaxhighlight>
 
=={{header|PL/I}}==
{{works with|IBM PL/I|7.5}}
<langsyntaxhighlight lang="pli">DCL (T(10)) FIXED BIN(31); /* scratch space of length N/2 */
 
MERGE: PROCEDURE (A,LA,B,LB,C);
Line 1,564 ⟶ 2,071:
CALL MERGE(T,M,AMP1,N-M,A);
RETURN;
END MERGESORT;</langsyntaxhighlight>
 
=={{header|Pop11}}==
Pop11 library function sorts lists. So we first convert array to list, then sort and finally convert back:
 
<langsyntaxhighlight lang="pop11">lvars ar = {2 4 3 1 2};
;;; Convert array to list.
;;; destvector leaves its results and on the pop11 stack + an integer saying how many there were
Line 1,579 ⟶ 2,086:
;;; Convert list to array
destlist(ls);
consvector() -> ar;</langsyntaxhighlight>
 
The above can be abbreviated to more economical, but possibly more opaque, syntax, using pop11 as a functional language:
 
<langsyntaxhighlight lang="pop11">lvars ar = {2 4 3 1 2};
consvector(destlist(sort(conslist(destvector(ar))))) -> ar;
;;; print the sorted vector:
ar =>
** {1 2 2 3 4}</langsyntaxhighlight>
 
(The list created by conslist will be garbage-collected.)
Line 1,593 ⟶ 2,100:
Alternatively, using the datalist function, even more economically:
 
<langsyntaxhighlight lang="pop11">lvars ar = {2 4 3 1 2};
consvector(destlist(sort(datalist(ar)))) -> ar;</langsyntaxhighlight>
 
or in Forth-like pop11 postfix syntax:
 
<langsyntaxhighlight lang="pop11">lvars ar = {2 4 3 1 2};
ar.datalist.sort.destlist.consvector -> ar;</langsyntaxhighlight>
 
=={{header|Potion}}==
<langsyntaxhighlight lang="potion">(7, 5, 1, 2, 3, 8, 9) sort join(", ") print</langsyntaxhighlight>
 
=={{header|PowerBASIC}}==
PowerBASIC has several options available for sorting. At its simplest, an array (of any type) is sorted using <code>ARRAY SORT</code>:
<syntaxhighlight lang ="powerbasic">ARRAY SORT x()</langsyntaxhighlight>
 
Options are available to limit sorting to only part of the array, collate string arrays, sort multiple arrays together, etc. (Details [http://www.powerbasic.com/support/help/pbwin/html/ARRAY_SORT_statement.htm here].)
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">34,12,23,56,1,129,4,2,73 | Sort-Object</langsyntaxhighlight>
 
=={{header|Prolog}}==
Line 1,620 ⟶ 2,127:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Dim numbers(20)
For i = 0 To 20
numbers(i) = Random(1000)
Next
 
SortArray(numbers(), #PB_Sort_Ascending)</langsyntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|2.3}}
<langsyntaxhighlight lang="python">nums = [2,4,3,1,2]
nums.sort()</langsyntaxhighlight>
 
'''Note:''' The array <tt>nums</tt> is sorted in place.
Line 1,638 ⟶ 2,145:
You could also use the built-in sorted() function
 
<langsyntaxhighlight lang="python">nums = sorted([2,4,3,1,2])</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
As a dialogue in the Quackery shell.
 
<pre>/O> [] 20 times [ 10 random join ]
... dup echo cr
... sort
... echo cr
...
[ 5 2 5 0 4 5 1 5 1 1 0 3 7 2 0 9 6 1 8 7 ]
[ 0 0 0 1 1 1 1 2 2 3 4 5 5 5 5 6 7 7 8 9 ]
</pre>
 
=={{header|R}}==
<langsyntaxhighlight lang="r">nums <- c(2,4,3,1,2)
sorted <- sort(nums)</langsyntaxhighlight>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
-> (sort '(1 9 2 8 3 7 4 6 5) <)
'(1 2 3 4 5 6 7 8 9)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
If <code>@a</code> contains only numbers:
 
<syntaxhighlight lang="raku" line>my @sorted = sort @a;</syntaxhighlight>
 
For an in-place sort:
 
<syntaxhighlight lang="raku" line>@a .= sort;</syntaxhighlight>
 
=={{header|Rascal}}==
Rascal has a built-in sort function that sort the elements of a list. Additionally, one can give a LessThenOrEqual function to compare the elements (See [http://tutor.rascal-mpl.org/Courses/Rascal/Rascal.html#/Courses/Rascal/Libraries/Prelude/List/sort/sort.html documentation]).
<langsyntaxhighlight lang="rascal">rascal>import List;
ok
 
Line 1,662 ⟶ 2,192:
 
rascal>sort(a, bool(int a, int b){return a >= b;})
list[int]: [5,4,3,2,1]</langsyntaxhighlight>
 
=={{header|Raven}}==
Sort list in place:
 
<langsyntaxhighlight lang="raven">[ 2 4 3 1 2 ] sort</langsyntaxhighlight>
 
=={{header|REBOL}}==
<langsyntaxhighlight lang="rebol">sort [2 4 3 1 2]</langsyntaxhighlight>
 
=={{header|Red}}==
<langsyntaxhighlight Redlang="red">>> nums: [3 2 6 4 1 9 0 5 7]
== [3 2 6 4 1 9 0 5 7]
>> sort nums
== [0 1 2 3 4 5 6 7 9]</langsyntaxhighlight>
 
=={{header|REXX}}==
===sort an array===
This REXX version creates an array with over a score of Euler numbers (integers), then sorts it.
<langsyntaxhighlight lang="rexx">/*REXX program sorts an array (using E─sort), in this case, the array contains integers.*/
numeric digits 30 /*enables handling larger Euler numbers*/
@. = 0; @.1 = 1
Line 1,709 ⟶ 2,239:
do j=1 for #; say _ arg(1) 'array element' right(j, w)"="right(@.j, 20)
end /*j*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default internal input:}}
<pre>
Line 1,763 ⟶ 2,293:
Because it so much more efficient to sort an array, &nbsp; an array is built from the list,
<br>it is then sorted, &nbsp; and then the list is re-constituted.
<langsyntaxhighlight lang="rexx">/*REXX program sorts (using E─sort) and displays a list of some interesting integers. */
Bell= 1 1 2 5 15 52 203 877 4140 21147 115975 /*a few Bell " */
Bern= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617' /*" " Bernoulli " */
Perrin= 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 /*" " Perrin " */
list= Bell Bern Perrin /*throw them all ───► a pot. */
say 'unsorted =' list /*display what's being shown.*/
size#= words(list) /*nice to have # of elements.*/
do j=1 for size# /*build an array, a single */
@.j=word(list, j) /* ··· element at a time.*/
end /*j*/
call eSort size# /*sort the collection of #s. */
$=; do k=1 for #; $= $ @.k /*list:build definea aslist nullfrom sothe fararray*/
do k=1 for size /*build a list from the array*/
$=$ @.k /*append a number to the list*/
end /*k*/
say ' sorted =' space($) /*display the sorted list. */
exit /*stick a fork in it, we're all done.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
eSort: procedure expose @.; parse arg N; h=N N /*an eXchange sort.*/
do while h>1; h= h %2 2 /*define a segment.*/
do i=1 for N-h; j= i; k= h +i i /*sort top segment.*/
do while @.k<@.j /*see if need swap.*/
parse value @.j @.k with @.k @.j /*swap two elements*/
if h>=j then leave; j= j - h; k= k -h h /*this part sorted?*/
end /*while @.k<@.j*/
end /*i*/
end /*while h>1*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default internal inputs:}}
 
<pre>
(Shown at &nbsp; <big>'''<sup>5</sup>/<sub>6</sub>'''</big> &nbsp; size.)
 
<pre style="font-size:83%">
unsorted = 1 1 2 5 15 52 203 877 4140 21147 115975 1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90
sorted = -3617 -691 -1 -1 -1 0 0 0 0 0 0 0 0 1 1 1 1 1 2 2 2 3 3 5 5 5 5 7 7 10 12 15 17 22 29 39 51 52 68 90 203 877 4140 21147 115975
Line 1,798 ⟶ 2,329:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">aArray = [2,4,3,1,2]
see sort(aArray)</langsyntaxhighlight>
 
=={{header|RPL}}==
{{works with|HP|48G}}
{2 4 3 1 2} SORT
{{out}}
<pre>
1: { 1 2 2 3 4 }
</pre>
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">nums = [2,4,3,1,2]
sorted = nums.sort # returns a new sorted array. 'nums' is unchanged
p sorted #=> [1, 2, 2, 3, 4]
Line 1,808 ⟶ 2,346:
 
nums.sort! # sort 'nums' "in-place"
p nums #=> [1, 2, 2, 3, 4]</langsyntaxhighlight>
 
=={{header|Rust}}==
Uses merge sort in place (undocumented), allocating ~2*n memory where n is a length of an array.
<langsyntaxhighlight lang="rust">fn main() {
let mut a = vec!(9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
 
a.sort();
println!("{:?}", a);
}</langsyntaxhighlight>
 
=={{header|Sather}}==
<syntaxhighlight lang="sather">class MAIN is
main is
arr: ARRAY{INT} := |4, 6, 7, 2, 1, 0, 100, 21, 34|;
#OUT+"unsorted: " + arr + "\n";
 
-- sort in place:
arr.sort;
 
#OUT+" sorted: " + arr + "\n";
end;
end;</syntaxhighlight>
 
Output:
<syntaxhighlight lang="text">unsorted: {4,6,7,2,1,0,100,21,34}
sorted: {0,1,2,4,6,7,21,34,100}</syntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
===Array===
Scala's "default" Array is a ''mutable'' data structure, very close to Java's Array. Generally speaking, that means an "array" is not very Scala-lesque, even as mutable data structures go. It can serves a purpose, though. If array is the right data type for your need, then that is how you sort it.<langsyntaxhighlight Scalalang="scala">import scala.compat.Platform
 
object Sort_an_integer_array extends App {
Line 1,836 ⟶ 2,391:
 
println(s"Array in sorted order.\nSuccessfully completed without errors. [total ${Platform.currentTime - executionStart} ms]")
}</langsyntaxhighlight>
===List===
<langsyntaxhighlight Scalalang="scala">println(List(5,2,78,2,578,-42).sorted)
//--> List(-42, 2, 2, 5, 78, 578)</langsyntaxhighlight>
 
=={{header|Scheme}}==
{{works with|Guile}}
Same as [[Common Lisp]]
<langsyntaxhighlight lang="scheme">(sort #(9 -2 1 2 8 0 1 2) #'<)</langsyntaxhighlight>
 
{{libheader|Scheme/SRFIs}}
Line 1,850 ⟶ 2,405:
Sorting is also available through SRFIs. SRFI 132 provides separate list-sort and vector-sort routines:
 
<langsyntaxhighlight lang="scheme">
> (import (srfi 132))
> (list-sort < '(9 -2 1 2 8 0 1 2))
Line 1,857 ⟶ 2,412:
> (vector-sort < #(9 -2 1 2 8 0 1 2))
#(-2 0 1 1 2 2 8 9)
</syntaxhighlight>
</lang>
 
SRFI 132 replaced the older SRFI 95, which is still found in many implementations. SRFI 95 provides a generic sort function (but note the order of the sequence and comparator!):
 
<langsyntaxhighlight lang="scheme">
> (import (srfi 95))
> (sort '(9 -2 1 2 8 0 1 2) <)
Line 1,867 ⟶ 2,422:
> (sort #(9 -2 1 2 8 0 1 2) <)
#(-2 0 1 1 2 2 8 9)
</syntaxhighlight>
</lang>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">var array integer: nums is [] (2, 4, 3, 1, 2);
 
nums := sort(nums);</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var nums = [2,4,3,1,2];
var sorted = nums.sort; # returns a new sorted array.
nums.sort!; # sort 'nums' "in-place"</langsyntaxhighlight>
 
=={{header|Slate}}==
<langsyntaxhighlight lang="slate"> #(7 5 2 9 0 -1) sort</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk"> #(7 5 2 9 0 -1) asSortedCollection</langsyntaxhighlight>
or destructive:
<langsyntaxhighlight lang="smalltalk"> #(7 5 2 9 0 -1) sort</langsyntaxhighlight>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "int_sort" )
@( description, "Sort an array (or list) of integers in ascending" )
@( description, "numerical order. Use a sorting facility provided by" )
@( description, "the language/library if possible." )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Sort_an_integer_array" );
pragma license( unrestricted );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure int_sort is
type int_array is array (1..9) of integer;
int_values : int_array := (0,1,8,2,7,3,6,4,5);
begin
arrays.heap_sort( int_values );
for i in arrays.first( int_values )..arrays.last( int_values ) loop
? int_values(i);
end loop;
end int_sort;</syntaxhighlight>
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="sparkling">var arr = { 2, 8, 1, 4, 6, 5, 3, 7, 0, 9 };
sort(arr);</langsyntaxhighlight>
 
=={{header|Standard ML}}==
Line 1,896 ⟶ 2,476:
===Array===
{{works with|SML/NJ}}
<langsyntaxhighlight lang="sml">- val nums = Array.fromList [2, 4, 3, 1, 2];
val nums = [|2,4,3,1,2|] : int array
- ArrayQSort.sort Int.compare nums;
val it = () : unit
- nums;
val it = [|1,2,2,3,4|] : int array</langsyntaxhighlight>
 
{{works with|Moscow ML}}
<langsyntaxhighlight lang="sml">- load "Arraysort";
> val it = () : unit
- load "Int";
Line 1,913 ⟶ 2,493:
> val it = () : unit
- Array.foldr op:: [] nums;
> val it = [1, 2, 2, 3, 4] : int list</langsyntaxhighlight>
 
===List===
{{works with|SML/NJ}}
<langsyntaxhighlight lang="sml">- val nums = [2, 4, 3, 1, 2];
val nums = [2,4,3,1,2] : int list
- val sorted = ListMergeSort.sort op> nums;
val sorted = [1,2,2,3,4] : int list</langsyntaxhighlight>
 
{{works with|Moscow ML}}
<langsyntaxhighlight lang="sml">- load "Listsort";
> val it = () : unit
- load "Int";
Line 1,930 ⟶ 2,510:
> val nums = [2, 4, 3, 1, 2] : int list
- val sorted = Listsort.sort Int.compare nums;
> val sorted = [1, 2, 2, 3, 4] : int list</langsyntaxhighlight>
 
=={{header|Stata}}==
Line 1,936 ⟶ 2,516:
See '''[https://www.stata.com/help.cgi?sort sort]''' in Stata help.
 
<langsyntaxhighlight lang="stata">. clear
. matrix a=(2,9,4,7,5,3,6,1,8)'
. qui svmat a
Line 1,955 ⟶ 2,535:
8. | 8 |
9. | 9 |
+----+</langsyntaxhighlight>
 
=== Sort a macro list ===
See '''[https://www.stata.com/help.cgi?macrolists macrolists]''' in Stata help for other functions on lists stored in macros.
 
<langsyntaxhighlight lang="stata">. local a 2 9 4 7 5 3 6 1 8
. di "`: list sort a'"
1 2 3 4 5 6 7 8 9</langsyntaxhighlight>
 
=== Mata ===
See Mata's '''[http://www.stata.com/help.cgi?mf_sort sort]''' function.
 
<langsyntaxhighlight lang="stata">mata
: a=2\9\4\7\5\3\6\1\8
 
Line 1,983 ⟶ 2,563:
9 | 9 |
+-----+
end</langsyntaxhighlight>
 
=={{header|Swift}}==
===Sort in place===
{{works with|Swift|2.x+}}
<langsyntaxhighlight lang="swift">var nums = [2, 4, 3, 1, 2]
nums.sortInPlace()
print(nums)</langsyntaxhighlight>
or
<langsyntaxhighlight lang="swift">var nums = [2, 4, 3, 1, 2]
nums.sortInPlace(<)
print(nums)</langsyntaxhighlight>
 
{{works with|Swift|1.x}}
<langsyntaxhighlight lang="swift">var nums = [2, 4, 3, 1, 2]
nums.sort(<)
println(nums)</langsyntaxhighlight>
or
<langsyntaxhighlight lang="swift">var nums = [2, 4, 3, 1, 2]
sort(&nums)
println(nums)</langsyntaxhighlight>
or
<langsyntaxhighlight lang="swift">var nums = [2, 4, 3, 1, 2]
sort(&nums, <)
println(nums)</langsyntaxhighlight>
 
===Return new array===
Line 2,013 ⟶ 2,593:
 
{{works with|Swift|2.x+}}
<langsyntaxhighlight lang="swift">let nums = [2,4,3,1,2].sort()
print(nums)</langsyntaxhighlight>
or
<langsyntaxhighlight lang="swift">let nums = [2,4,3,1,2].sort(<)
print(nums)</langsyntaxhighlight>
 
{{works with|Swift|1.x}}
<langsyntaxhighlight lang="swift">let nums = sorted([2,4,3,1,2])
println(nums)</langsyntaxhighlight>
or
<langsyntaxhighlight lang="swift">let nums = [2,4,3,1,2].sorted(<)
println(nums)</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">set result [lsort -integer $unsorted_list]</langsyntaxhighlight>
 
=={{header|TI-83 BASIC}}==
Line 2,038 ⟶ 2,618:
This can be done by using the bubble sort library:
 
<langsyntaxhighlight lang="toka">needs bsort
arrayname number_elements bsort</langsyntaxhighlight>
 
See the Toka entry on [[Bubble Sort]] for a full example.
Line 2,046 ⟶ 2,626:
Each shell parameter separates the integers using the default IFS whitespace (space, tab, newline).
 
<langsyntaxhighlight lang="bash">nums="2 4 3 1 5"
sorted=`printf "%s\n" $nums | sort -n`
echo $sorted # prints 1 2 3 4 5</langsyntaxhighlight>
 
Alternate solution: <tt>sorted=`for i in $nums; do echo $i; done | sort -n`</tt>
Line 2,056 ⟶ 2,636:
 
{{works with|pdksh|5.2.14}}
<langsyntaxhighlight lang="bash">set -A nums 2 4 3 1 5
set -A sorted $(printf "%s\n" ${nums[*]} | sort -n)
echo ${sorted[*]} # prints 1 2 3 4 5</langsyntaxhighlight>
 
Users of [[bash]], [[ksh93]] and [[mksh]] can probably use the <tt>nums=(2 4 3 1 2)</tt> syntax.
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">decl int<> nums
append 2 4 3 1 2 nums
sort nums</langsyntaxhighlight>
 
=={{header|Ursala}}==
using the built in sort operator, -<, with the nleq library function
for comparing natural numbers
<langsyntaxhighlight Ursalalang="ursala">#import nat
 
#cast %nL
 
example = nleq-< <39,47,40,53,14,23,88,52,78,62,41,92,88,66,5,40></langsyntaxhighlight>
output:
<pre><5,14,23,39,40,40,41,47,52,53,62,66,78,88,88,92></pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
mut nums := [3, 2, 4, 1, 6, 7, 5, 0]
nums.sort()
println(nums)
}</syntaxhighlight>
 
{{out}}
<pre>
[0, 1, 2, 3, 4, 5, 6, 7]
</pre>
 
=={{header|WDTE}}==
<langsyntaxhighlight WDTElang="wdte">let a => import 'arrays';
a.sort [39; 47; 40; 53; 14; 23; 88; 52; 78; 62; 41; 92; 88; 66; 5; 40] < -- io.writeln io.stdout;</langsyntaxhighlight>
 
=={{header|Wortel}}==
<langsyntaxhighlight lang="wortel">@sort [39 47 40 53 14 23 88 52 78 62 41 92 88 66 5 40]</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="wren">import "./sort" for Sort
 
var a = [7, 10, 2, 4, 6, 1, 8, 3, 9, 5]
Sort.quick(a)
System.print(a)</syntaxhighlight>
 
{{out}}
<pre>
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
proc SSort(A, N); \Shell sort array in ascending order
Line 2,112 ⟶ 2,717:
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )];
CrLf(0);
]</langsyntaxhighlight>
 
Output:
Line 2,120 ⟶ 2,725:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">export sub shell_sort(x())
// Shell sort based on insertion sort
 
Line 2,175 ⟶ 2,780:
next n
 
end if</langsyntaxhighlight>
 
=={{header|Yorick}}==
In Yorick, ''sort'' returns an index list into the array that will put it in sorted order.
<langsyntaxhighlight lang="yorick">
nums = [2,4,3,1,2];
nums = nums(sort(nums));
</syntaxhighlight>
</lang>
 
=={{header|zkl}}==
In place sorting read/write list:
<langsyntaxhighlight lang="zkl">a:=L(4,5,2,6); a.sort(); a.println() //--> L(2,4,5,6)</langsyntaxhighlight>
Sort a read only list:
<langsyntaxhighlight lang="zkl">a:=T(4,5,2,6); b:=a.sort();
b.println(); //--> L(2,4,5,6)
a.println(); //--> L(4,5,2,6)</langsyntaxhighlight>
 
=={{header|Zoea}}==
<syntaxhighlight lang="zoea">
program: sort_integer_array
input: [2,4,3,1]
output: [1,2,3,4]
</syntaxhighlight>
 
=={{header|Zoea Visual}}==
[http://zoea.co.uk/examples/zv-rc/Sort_integer_array.png Sort integer array]
9,476

edits