Sort three variables: Difference between revisions

(Add CLU)
 
(17 intermediate revisions by 15 users not shown)
Line 66:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">V x = 77444
V y = -12
V z = 0
Line 76:
V zs = ‘(from the "Wizard of OZ")’
(xs, ys, zs) = sorted([xs, ys, zs])
print(xs"\n"ys"\n"zs)</langsyntaxhighlight>
 
{{out}}
Line 85:
lions, tigers, and
</pre>
 
=={{header|8086 Assembly}}==
===Sorting Unsigned Integers===
<syntaxhighlight lang="asm">mov ax,6FFFh
mov bx,3456h
mov cx,0
 
;We'll consider these sorted when ax <= bx <= cx.
 
SortRegisters proc
cmp ax,bx
jbe continue
;if we got here, ax > bx. We don't know the relationship between bx and cx at this time.
cmp ax,cx
jbe swap_ax_and_bx
;if we got here, ax > bx, and bx > cx. Therefore all we need to do is swap ax and cx, and we're done.
xchg ax,cx
jmp endOfProc
 
swap ax_and_bx:
;if we got here, ax > bx, and ax <= cx. So all we have to do is swap ax and bx, and we're done
xchg ax,bx
jmp end ;back to top
 
continue: ;if we got here, ax <= bx.
cmp bx,cx
jbe end
;if we got here, ax <= bx, and bx > cx. Therefore all we need to do is swap bx and cx, and we're done.
xchg bx,cx
 
 
endOfProc: ;if we got here, ax <= bx, and bx <= cx. Therefore, ax <= bx <=cx and we are done.
;
 
SortRegisters endp </syntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Strings.Unbounded;
 
Line 150 ⟶ 186:
Test_Unbounded_Sort;
Test_Integer_Sort;
end Sort_Three;</langsyntaxhighlight>
 
{{out}}
Line 162 ⟶ 198:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">integer a, b, c;
index i;
text x, y, z;
Line 189 ⟶ 225:
c = i.if_pick;
 
o_form("~\n~\n~\n", a, b, c);</langsyntaxhighlight>
{{out}}
<pre>(from the "Wizard of OZ")
Line 205 ⟶ 241:
<br>
As the task only requires sorting three values, we use a simple, three-element specific sort routine.
<langsyntaxhighlight lang="algol68">BEGIN
# MODE that can hold integers and strings - would need to be extended to #
# allow for other types #
Line 254 ⟶ 290:
sort 3( x, y, z );
print( ( x, newline, y, newline, z, newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>(from the "Wizard of OZ")
Line 262 ⟶ 298:
+0
+77444</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
 
#include <basico.h>
 
algoritmo
 
x = 77444, y = -12.5, z = 0
low=x, midd=y, high=z
imprimir("BEFORE:\nX = ",x, " Y = ",y, " Z = ",z,NL)
#basic{
x = min( min( low,midd),high)
z = max( max( low,midd),high)
y = low + midd + high - x - z
}
 
imprimir("\nAFTER:\nX = ",x, " Y = ",y, " Z = ",z,NL,NL)
 
x = "lions, tigers, and"
y = "bears, oh my!"
z = "(from the \"Wizard of OZ\")"
imprimir("BEFORE:\nX = ",x, "\nY = ",y, "\nZ = ",z,NL)
#(x > y), entonces { intercambiar( x,y) }
#(y > z), entonces {
intercambiar (y,z )
#(x > y), entonces { intercambiar( x,y ) }
}
 
imprimir("\nAFTER:\nX = ",x, "\nY = ",y, "\nZ = ",z,NL,NL)
 
p = {}
'"lions, tigers, and",77444,"bears, oh my!",-12.7,0,"(from the \"Wizard of OZ\")"'
enlistar en 'p'
 
fijar separador 'NL'
imprimir("BEFORE:\n",p,NL)
matriz.ordenar(p)
imprimir("\nAFTER:\n",p,NL)
 
 
terminar
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/sort3var.bas
BEFORE:
X = 77444 Y = -12.500000 Z = 0
 
AFTER:
X = -12.500000 Y = 0.000000 Z = 77444.000000
 
BEFORE:
X = lions, tigers, and
Y = bears, oh my!
Z = (from the "Wizard of OZ")
 
AFTER:
X = (from the "Wizard of OZ")
Y = bears, oh my!
Z = lions, tigers, and
 
BEFORE:
lions, tigers, and
77444
bears, oh my!
-12.700000
0
(from the "Wizard of OZ")
 
AFTER:
(from the "Wizard of OZ")
bears, oh my!
lions, tigers, and
-12.700000
0
77444
 
</pre>
 
=={{header|APL}}==
Line 267 ⟶ 385:
Works in [[Dyalog APL]]. Assumes x,y,z are already defined.
 
<langsyntaxhighlight lang="apl">x y z←{⍵[⍋⍵]}x y z</langsyntaxhighlight>
 
Uses destructuring assignment syntax.
Line 273 ⟶ 391:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">set x to "lions, tigers, and"
set y to "bears, oh my!"
set z to "(from the \"Wizard of OZ\")"
Line 283 ⟶ 401:
end if
 
return {x, y, z}</langsyntaxhighlight>
 
{{output}}
<pre>{"(from the \"Wizard of OZ\")", "bears, oh my!", "lions, tigers, and"}</pre>
 
<langsyntaxhighlight lang="applescript">set x to 77444
set y to -12
set z to 0
Line 298 ⟶ 416:
end if
 
return {x, y, z}</langsyntaxhighlight>
 
{{output}}
Line 305 ⟶ 423:
Longer, but more efficiently:
 
<langsyntaxhighlight lang="applescript">set x to "lions, tigers, and"
set y to "bears, oh my!"
set z to "(from the \"Wizard of OZ\")"
Line 327 ⟶ 445:
end tell
 
return {x, y, z}</langsyntaxhighlight>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">x: {lions, tigers, and}
y: {bears, oh my!}
z: {(from the "Wizard of OZ")}
Line 340 ⟶ 458:
z: pi
 
print sort @[x y z]</langsyntaxhighlight>
 
{{out}}
Line 350 ⟶ 468:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">SortThreeVariables(ByRef x,ByRef y,ByRef z){
obj := []
for k, v in (var := StrSplit("x,y,z", ","))
Line 356 ⟶ 474:
for k, v in obj
temp := var[A_Index], %temp% := k
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">x = lions, tigers, and
y = bears, oh my!
z = (from the "Wizard of OZ")
Line 368 ⟶ 486:
SortThreeVariables(x,y,z)
MsgBox % x "`n" y "`n" z
return</langsyntaxhighlight>
Outputs:<pre>---------------------------
(from the "Wizard of OZ")
Line 391 ⟶ 509:
all one would need to do is define a third comparator.
 
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
// Sort 3 variables using a comparator.
Line 438 ⟶ 556:
writes("Y = ") ; printfn(y) ; wrch('*N')
writes("Z = ") ; printfn(z) ; wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre>X = 7444
Line 463 ⟶ 581:
 
===Generic Implementation===
<syntaxhighlight lang="c">
<lang C>
#include<string.h>
#include<stdlib.h>
Line 513 ⟶ 631:
return 0;
}
</langsyntaxhighlight>The output shows three test cases, two as specified in the task, and one which mixes numbers and strings. The output is sorted considering all of them as strings in that case.
<pre>
Enter 1st value : 77444
Line 537 ⟶ 655:
</pre>
===Task Specific Implementation===
<syntaxhighlight lang="c">
<lang C>
#include<stdio.h>
 
Line 565 ⟶ 683:
return 0;
}
</syntaxhighlight>
</lang>
Output :
<pre>Before sorting :
Line 578 ⟶ 696:
=={{header|C sharp}}==
{{works with|C sharp|7.0}}
<langsyntaxhighlight lang="csharp">using System;
public class Program
{
Line 608 ⟶ 726:
if (b.CompareTo(c) > 0) (b, c) = (c, b);
}
}</langsyntaxhighlight>
{{out}}
<pre>(-12, 0, 77444)
Line 614 ⟶ 732:
 
=={{header|C++}}==
<langsyntaxhighlight Cpplang="cpp">#include <algorithm>
#include <iostream>
#include <string>
Line 641 ⟶ 759:
std::cout << xf << "\n" << yf << "\n" << zf << "\n\n";
}
</syntaxhighlight>
</lang>
{{out}}
<pre>-12
Line 656 ⟶ 774:
 
=={{header|CLU}}==
<langsyntaxhighlight CLUlang="clu">% Sort three variables.
% The variables must all be of the same type, and the type
% must implement the less-than comparator.
Line 704 ⟶ 822:
example[string] ("lions, tigers and", "bears, oh my!",
"(from the \"Wizard of Oz\")", fmt_str)
end start_up</langsyntaxhighlight>
{{out}}
<pre>x=77444 y=-12 z=0
Line 716 ⟶ 834:
 
=={{header|COBOL}}==
<langsyntaxhighlight COBOLlang="cobol"> program-id. 3var.
data division.
working-storage section.
Line 758 ⟶ 876:
stop run
.
end program 3var.</langsyntaxhighlight>
{{out}}
<pre>x = (from the "Wizard of OZ")
Line 770 ⟶ 888:
=={{header|Cowgol}}==
 
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# Sort 3 values
Line 818 ⟶ 936:
 
# Print 3 values after sorting
print3(x, y, z);</langsyntaxhighlight>
 
{{out}}
Line 826 ⟶ 944:
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.stdio;
 
void main() {
Line 860 ⟶ 978:
swap(y,z);
}
}</langsyntaxhighlight>
 
{{out}}
Line 867 ⟶ 985:
BEFORE: x=[lions, tigers, and]; y=[bears, oh my!]; z=[(from the "Wizard of OZ")]
AFTER: x=[(from the "Wizard of OZ")]; y=[bears, oh my!]; z=[lions, tigers, and]</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
proc sort3 . a b c .
if a > c
swap a c
.
if b > c
swap b c
.
if a > b
swap a b
.
.
x = 77444
y = -12
z = 0
sort3 x y z
print x & " " & y & " " & z
#
proc sort3str . a$ b$ c$ .
if strcmp a$ c$ > 0
swap a$ c$
.
if strcmp b$ c$ > 0
swap b$ c$
.
if strcmp a$ b$ > 0
swap a$ b$
.
.
x$ = "lions, tigers, and"
y$ = "bears, oh my!"
z$ = "(from the \"Wizard of OZ\")"
sort3str x$ y$ z$
print x$
print y$
print z$
</syntaxhighlight>
 
 
=={{header|EDSAC order code}}==
<syntaxhighlight lang="edsac">
[Sort three variables, for Rosetta Code.
EDSAC, Initial Orders 2]
[---------------------------------------------------------------------------
Sorts three 35-bit variables x, y, x, stored at 0#V, 2#V, 4#V respectively.
Uses the algortihm:
if x > z then swap( x,z)
if y > z then swap( y,z)
if x > y then swap( x,y)
At most two swaps are carried out in any particular case.
----------------------------------------------------------------------------]
[Arrange the storage]
T47K P96F [M parameter: main routine]
T55K P128F [V parameter: variables to be sorted]
 
[Compressed form of library subroutine R2.
Reads integers at load time and is then overwritten.]
GKT20FVDL8FA40DUDTFI40FA40FS39FG@S2FG23FA5@T5@E4@E13Z
T#V [tell R2 where to store integers]
[EDIT: List of 35-bit integers separated by 'F', list terminated by '#TZ'.]
987654321F500000000F123456789#TZ
 
[Main routine]
E25K TM GK
[0] A4#V S#V [accumulator := z - x]
E8@ [skip the swap if x <= z]
TD [0D := z - x]
A#V U4#V [z := x]
AD [acc := old z]
T#V [x := old z]
[8] TF [clear acc]
A4#V S2#V [acc := z - y]
E17@ [skip the swap if y <= z]
TD [0D := z - y]
A2#V U4#V [z := y]
AD [acc := old z]
T2#V [y := old z]
[17] TF [clear acc]
A2#V S#V [acc := y - x]
E26@ [skip the swap if x <= y]
TD [0D := y - x]
A#V U2#V [y := x]
AD [acc := old y]
T#V [x := old y]
[26] ZF [halt the machine]
EZ [define entry point]
PF [acc = 0 on entry]
[end]
</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 56.0x :
<langsyntaxhighlight lang="elena">import extensions;
sortThree(ref object a, ref object b, ref object c)
Line 894 ⟶ 1,103:
console.printLine(x,",",y,",",z);
console.printLine(a,",",b,",",c)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 902 ⟶ 1,111:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">x = 'lions, tigers, and'
y = 'bears, oh my!'
z = '(from the "Wizard of OZ")'
Line 914 ⟶ 1,123:
 
[x, y, z] = Enum.sort([x, y, z])
IO.puts "x = #{x}\ny = #{y}\nz = #{z}"</langsyntaxhighlight>
{{out}}
<pre>x = (from the "Wizard of OZ")
Line 925 ⟶ 1,134:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp"> let x = "lions, tigers, and"
let y = "bears, oh my!"
let z = """(from the "Wizard of OZ")"""
List.iter (printfn "%s") (List.sort [x;y;z])
</syntaxhighlight>
</lang>
{{out}}
<pre>(from the "Wizard of OZ")
Line 936 ⟶ 1,145:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: arrays io kernel prettyprint sequences sorting ;
IN: rosetta-code.sort-three
 
Line 946 ⟶ 1,155:
sort3 [ print ] tri@
 
77444 -12 0 sort3 [ . ] tri@</langsyntaxhighlight>
{{out}}
<pre>(from the "Wizard of OZ")
Line 954 ⟶ 1,163:
0
77444</pre>
 
=={{header|FALSE}}==
 
FALSE supports neither floating point numbers nor strings.
 
<syntaxhighlight lang="false">{ [s]waps two variables. (varref; varref) → () }
[
b: { loads the value at the top of the stack into b }
a: { loads the value at the top of the stack into a }
 
a;; t: { loads the value stored in the variable stored in a (hence the double dereference) into t }
b;; a;: { loads the value stored in the variable stored in b into the variable stored in a }
t; b;: { loads the value stored in t into the variable stored in b }
]s:
 
{ [p]rints the three variables. }
[
"X = " x;. 10,
"Y = " y;. 10,
"Z = " z;. 10,
]p:
 
77444 x:
12_ y:
0 z:
 
p;!
 
{ if x > y, swap x and y }
x;y;> [xys;!] ?
 
{ if y > z, swap y and z }
y;z;> [yzs;!] ?
 
{ if x > y, swap x and y }
x;y;> [xys;!] ?
 
"After sorting:
"
 
p;!</syntaxhighlight>
 
{{out}}
<pre>X = 77444
Y = -12
Z = 0
After sorting:
X = -12
Y = 0
Z = 77444</pre>
 
=={{header|FreeBASIC}}==
Shows the use of macros, which are type-agnostic (though you cannot mix string and numerical types).
<langsyntaxhighlight lang="freebasic">#macro sort_three( x, y, z )
if x>y then swap x, y
if y>z then swap y, z
Line 982 ⟶ 1,241:
print a
print b
print c</langsyntaxhighlight>
{{out}}<pre>(from the "Wizard of OZ")
bears, oh my!
Line 1,000 ⟶ 1,259:
typically be used directly from the stack by a subsequent operation in the
program.
<LANGsyntaxhighlight lang="forth" lines>\ sort 3 integers
VARIABLE X VARIABLE Y VARIABLE Z
 
Line 1,009 ⟶ 1,268:
2DUP < IF SWAP THEN ;
 
: SORT3INTS ( a b c -- c b a) ?SWAP >R ?SWAP R> ?SWAP ;</LANGsyntaxhighlight>
 
Testing is done using the Forth console using '?' to view VARIABLE contents
Line 1,024 ⟶ 1,283:
==={{header|Strings}}===
Strings require extending the language but the primitives needed are part of ANS/ISO Forth.
<LANGsyntaxhighlight lang="forth" lines>DECIMAL
: BUFFER: ( n -- ) CREATE ALLOT ;
 
Line 1,049 ⟶ 1,308:
\ non-destructive print 3 counted-strings from data stack
: .STRS ( caddr1 caddr2 caddr3 -- caddr1 caddr2 caddr3) \ order is dependant
3 0 DO ROT DUP CR COUNT TYPE LOOP ; </LANGsyntaxhighlight>
With these extensions we can do the same testing at the Forth console and
examine the string order with '.STRS'.
Line 1,077 ⟶ 1,336:
There would be similar requirements for a SWAP routine for each type of parameter, for alas, Fortran does not define a SWAP statement. The temporary variable needed for the SWAP process is defined rather intimidatingly as having the size of the largest of the three parameters; prior to F90 variables defined in a routine could only have a size defined at compile time, which would have to be "surely big enough". Rather than have this redefined for each invocation of SWAPC (where it would be the larger of the two parameters) it is defined once in SORT3. However, all three parameters should be the same size, or risk truncation. Using a great deal more syntax (or, as standardised in F2003) it is possible to have character variables be resized on each assignment to them to accommodate the length of text being assigned.
 
One could make rather more use of the facilities of F90 and define a compound data type, such as <langsyntaxhighlight Fortranlang="fortran"> TYPE(MONGREL)
INTEGER TYPEIS
INTEGER VI
Line 1,084 ⟶ 1,343:
...etc...
END TYPE MONGREL
TYPE (MONGREL) DOG </langsyntaxhighlight>
So that DOG.TYPEIS would indicate which component to access. Still further ploys would enable storage to be allocated only for the type currently in use, especially for CHARACTER variables, and via a great deal more syntax defining how to perform operations such as .GT. and the like, MONGREL type variables can be used in expressions involving such operators just as can variables of the basic types. This sort of approach is demonstrated in [[Arithmetic/Rational#Fortran]], but otherwise, every reference to a MONGREL will involve some sort of CASE statement to select the appropriate usage, hopefully of related types only. Serious computation with such MONGREL variables surely will not be speedy and thus would be un-Fortrannish. What to do when a text string meets a complex number remains obscure - convert the number to a text (but, what format?), does the text represent a number? And what type results from such miscegnation?
 
Routines that modify their parameters should not be invoked with constants (or text literals) as such parameters... Some systems allow constants to be in protected storage, and if so, an attempt to modify such storage will produce a run-time error. Otherwise, it all depends on how constants are passed as parameters. If a temporary storage item is loaded with the desired value and the address of that scratch variable is passed, then disaster will be averted - though good results may not be produced.
 
For convenience in setting up the two examples, an array is used to hold the test data. The subroutine is not invoked with an array parameter, it is invoked with three separate elements of the array. The DATA statement initialising the array looks to be the transpose of the desired ordering, because of the way Fortran orders elements in storage. <langsyntaxhighlight Fortranlang="fortran"> SUBROUTINE SORT3(X,Y,Z) !Perpetrate a bubblesort in-line.
CHARACTER*(*) X,Y,Z !Just three to rearrange.
CHARACTER*(MAX(LEN(X),LEN(Y),LEN(Z))) T !Really, they should all be the same length.
Line 1,119 ⟶ 1,378:
END DO !On to the next example.
END !Nothing much.
</syntaxhighlight>
</lang>
Output: the texts showing numbers appear in text order, not the order of their numbers. Incidentally, not everything is done in ASCII. The EBCDIC ordering is different.
<pre> Supplied: >lions, tigers, and < >bears, oh my! < >(from the "Wizard of OZ") <
Line 1,128 ⟶ 1,387:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sort_three_variables}}
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.
 
'''Test case 1'''
In '''[https://formulae.org/?example=Sort_three_variables this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Sort three variables 01.png]]
 
[[File:Fōrmulæ - Sort three variables 02.png]]
 
'''Test case 2'''
 
[[File:Fōrmulæ - Sort three variables 03.png]]
 
[[File:Fōrmulæ - Sort three variables 04.png]]
 
=={{header|Free Pascal}}==
The FPC (FreePascal Compiler) adopted Delphi’s generics scheme.
It works in the <tt>{$mode objFPC}</tt> and <tt>{$mode Delphi}</tt> compiler compatibility modes, the former requiring the keywords <tt>generic</tt>/<tt>specialize</tt>.
<langsyntaxhighlight lang="delphi">{$mode objFPC}
 
generic procedure sort<T>(var X, Y: T);
Line 1,195 ⟶ 1,464:
specialize sort<real>(P, Q, R);
specialize print<real>(P, Q, R)
end.</langsyntaxhighlight>
{{out}}
<pre>X = (from the "Wizard of OZ")
Line 1,208 ⟶ 1,477:
Y = 9.0099999999999998E+000
Z = 1.2340000000000000E+001</pre>
 
=={{header|Frink}}==
The following sorts the values in the variables x, y, and z and sets the sorted values back to the variables.
 
<syntaxhighlight lang="frink">x = 77444
y = -12
z = 0
 
[x,y,z] = sort[[x,y,z]]
println["x = $x"]
println["y = $y"]
println["z = $z"]</syntaxhighlight>
{{out}}
<pre>
x = -12
y = 0
z = 77444
</pre>
 
=={{header|Go}}==
There are ways of doing this task in a generic way in Go but they are a bit cumbersome and it would not be idiomatic. Shown here then are solutions coded specifically to the string and integer types of the task test cases. Solutions would be very similar for any of the other comparable Go types such as float64.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,385 ⟶ 1,672:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>sorted strings:
Line 1,405 ⟶ 1,692:
Although mutation is not on the menu, parameterised types do allow us to define polymorphic functions which can, for example, be applied both to lists of strings and also to lists of integers or lists of floats. The following functions work with triples of any type for which the '''<=''' or '''compare''' functions are defined – in other words, any type for which an instance of the Ord class is defined.
 
<langsyntaxhighlight Haskelllang="haskell">import Data.List (sort)
 
sortedTriple
Line 1,429 ⟶ 1,716:
("lions, tigers, and", "bears, oh my!", "(from the \"Wizard of OZ\")")
print $ sortedTriple (77444, -12, 0)
print $ sortedListfromTriple (77444, -12, 0)</langsyntaxhighlight>
{{Out}}
<pre>("(from the \"Wizard of OZ\")","bears, oh my!","lions, tigers, and")
Line 1,437 ⟶ 1,724:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 LET X=77444:LET Y=-12:LET Z=0
110 PRINT X;Y;Z
120 CALL SHORT(X,Y,Z)
Line 1,445 ⟶ 1,732:
160 IF B>C THEN LET T=B:LET B=C:LET C=T
170 IF A>B THEN LET T=A:LET A=B:LET B=T
180 END DEF</langsyntaxhighlight>
{{out}}
<pre> 77444 -12 0
Line 1,452 ⟶ 1,739:
=={{header|J}}==
Note that this is extremely bad form, and you will stumble over why it is bad form if you try using it in any useful implementation:
<langsyntaxhighlight Jlang="j"> x =: 'lions, tigers, and'
y =: 'bears, oh my!'
z =: '(from the "Wizard of OZ")'
Line 1,472 ⟶ 1,759:
0
z
77444</langsyntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
<lang Java>
import java.util.Comparator;
import java.util.stream.Stream;
Line 1,518 ⟶ 1,805:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Sorted values: 11 21 82
Line 1,525 ⟶ 1,812:
 
=={{header|JavaScript}}==
<langsyntaxhighlight JavaScriptlang="javascript">const printThree = (note, [a, b, c], [a1, b1, c1]) => {
console.log(`${note}
${a} is: ${a1}
Line 1,551 ⟶ 1,838:
};
sortThree();
</syntaxhighlight>
</lang>
{{out}}
<pre>Before Sorting
Line 1,580 ⟶ 1,867:
The sorting function defined here is completely generic with respect to the number of variables and the types of their values. For brevity, however, we will only use the required examples.
 
<langsyntaxhighlight lang="jq">def example1:
{x: "lions, tigers, and",
y: "bears, oh my",
Line 1,590 ⟶ 1,877:
y: -12,
z: 0
};</langsyntaxhighlight>
 
The following sorting function will accept an arbitrary JSON object:
<syntaxhighlight lang="text">def sortVariables:
keys_unsorted as $keys
| ([.[]] | sort) as $values
| reduce range(0; $keys|length) as $i ({}; .[$keys[$i]] = ($values[$i]) ) ;</langsyntaxhighlight>
 
Examples:
<syntaxhighlight lang ="jq">example1 | sortVariables</langsyntaxhighlight>
{{out}}
<pre>{
Line 1,607 ⟶ 1,894:
}
</pre>
<syntaxhighlight lang ="jq">example2 | sortVariables</langsyntaxhighlight>
{{out}}<pre>
{
Line 1,616 ⟶ 1,903:
 
=={{header|Jsish}}==
<langsyntaxhighlight lang="javascript">#!/usr/bin/env jsish
/* Sort three variables, in Jsish. semi-colon start/end for unit test echo */
 
Line 1,703 ⟶ 1,990:
z ==> 1 string
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 1,736 ⟶ 2,023:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># v0.6
 
a, b, c = "lions, tigers, and", "bears, oh my!", "(from the \"Wizard of OZ\")"
Line 1,744 ⟶ 2,031:
a, b, c = 77444, -12, 0
a, b, c = sort([a, b, c])
@show a b c</langsyntaxhighlight>
 
{{out}}
Line 1,756 ⟶ 2,043:
=={{header|Kotlin}}==
Kotlin's standard library contains a sort() function which can sort any generic array whose element type has a defined ordering between its instances. This includes strings, integers and floats examples of which are shown below:
<langsyntaxhighlight lang="scala">// version 1.1.2
 
inline fun <reified T : Comparable<T>> sortThree(x: T, y: T, z: T): Triple<T, T, T> {
Line 1,793 ⟶ 2,080:
z3 = t3.third
printThree(x3, y3, z3)
}</langsyntaxhighlight>
 
{{out}}
Line 1,809 ⟶ 2,096:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 1,915 ⟶ 2,202:
_arraysort arrayl
_outofarray x y z arrayl
printf "Sorted Variables:\n%s\n%s\n%s\n\n" "${x}" "${y}" "${z}"</langsyntaxhighlight>
{{out}}<pre>
Numeric Variables:
Line 1,939 ⟶ 2,226:
=={{header|Little Man Computer}}==
The LMC in its original form supports only integers in the range 0..999. So the numbers in the task have been changed here from 77444, -12, 0 to 774, 0, 12.
<syntaxhighlight lang="little man computer">
<lang Little Man Computer>
// Little Man Computer
// Sort x, y, z, in ascending order
Line 2,000 ⟶ 2,287:
t DAT
// end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,014 ⟶ 2,301:
=={{header|Lua}}==
Essentially the same as the example algorithm except that the function is variadic. Sorting is done by the in-built table.sort and copying the unknown number of variables in and out of the table is made simple by the (...) in the parameter list and the unpack function respectively.
<langsyntaxhighlight Lualang="lua">function variadicSort (...)
local t = {}
for _, x in pairs{...} do
Line 2,039 ⟶ 2,326:
print("\ty = " .. y)
print("\tz = " .. z)
end</langsyntaxhighlight>
{{out}}
<pre>Case 1
Line 2,061 ⟶ 2,348:
Modules and Functions have a special list, where included at definition time, end excluded when parent exit.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Sort3 {
Let X=77744, Y=-12, Z=0
Line 2,093 ⟶ 2,380:
}
Sort3
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">lst := sort([x,y,z]):
x,y,z := lst[1],lst[2],lst[3]:</langsyntaxhighlight>
{{Out|Example}}
x := 'lions, tigers, and':
Line 2,120 ⟶ 2,407:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
All variables in Mathematica are typeless so the follow code works in all cases
<langsyntaxhighlight Mathematicalang="mathematica">{x, y, z} = Sort[{x, y, z}]</langsyntaxhighlight>
example:
<langsyntaxhighlight Mathematicalang="mathematica">x = 77444;
y = -12;
z = 0;
{x, y, z} = Sort[{x, y, z}];
{x, y, z}</langsyntaxhighlight>
{{out}}
<pre>{-12, 0, 77444}</pre>
Line 2,133 ⟶ 2,420:
{{works with|min|0.19.3}}
The <code>sort3</code> operator updates the values of the variables given to it in the calling scope, much like passing by reference. A small drawback is that we must quote the symbols passed in, lest we lose access to them.
<langsyntaxhighlight lang="min">(=c =b =a (a -> b -> c ->) => '> sort -> c @ b @ a @) :sort3
 
"lions, tigers, and" :x
Line 2,151 ⟶ 2,438:
x puts!
y puts!
z puts!</langsyntaxhighlight>
{{out}}
<pre>
Line 2,164 ⟶ 2,451:
=={{header|Modula-2}}==
{{Output?}}
<langsyntaxhighlight lang="modula2">MODULE SortThreeVariables;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,207 ⟶ 2,494:
 
ReadChar;
END SortThreeVariables.</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight lang="nanoquery">import sort
 
// sorting string literals
Line 2,234 ⟶ 2,521:
y = varlist[1]
z = varlist[2]
println x; println y; println z</langsyntaxhighlight>
{{out}}
<pre>(from the "Wizard of OZ")
Line 2,244 ⟶ 2,531:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">proc sortThree[T](a, b, c: var T) =
# Bubble sort, why not?
while not (a <= b and b <= c):
Line 2,258 ⟶ 2,545:
testWith(6, 4, 2)
testWith(0.9, -37.1, 4.0)
testWith("lions", "tigers", "bears")</langsyntaxhighlight>
 
{{out}}
Line 2,269 ⟶ 2,556:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let sortrefs list =
let sorted = List.map ( ! ) list
|> List.sort (fun a b ->
Line 2,296 ⟶ 2,583:
printf "\tx: %d\n" !x;
printf "\ty: %d\n" !y;
printf "\tz: %d\n" !z</langsyntaxhighlight>
 
{{out}}
Line 2,315 ⟶ 2,602:
The data types of all variables and routine parameters need to be known in advance.
The following <tt>program</tt> demonstrates sorting three strings, but can be easily adapted to work for <tt>integer</tt> and <tt>real</tt> variables as well.
<langsyntaxhighlight lang="pascal">program sortThreeVariables(output);
 
type
Line 2,352 ⟶ 2,639:
procedure printThreeLines(protected X, Y, Z: line);
begin
{ `protected` paremetersparameters cannot be overwritten }
writeLn('X = ', X);
writeLn('Y = ', Y);
Line 2,369 ⟶ 2,656:
sortThreeLines(A, B, C);
printThreeLines(A, B, C)
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
Line 2,375 ⟶ 2,662:
{{works with|Perl|5.10+}}
 
<langsyntaxhighlight Perllang="perl">#!/usr/bin/env perl
use 5.010_000;
 
Line 2,405 ⟶ 2,692:
say " x = $x";
say " y = $y";
say " z = $z";</langsyntaxhighlight>
 
{{out}}
Line 2,420 ⟶ 2,707:
=={{header|Phix}}==
Phix is naturally polymorphic
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>object {x,y,z} = {"lions, tigers, and","bears, oh my","(from the \"Wizard of OZ\")"}
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
?{x,y,z}
<span style="color: #004080;">object</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"lions, tigers, and"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"bears, oh my"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(from the \"Wizard of OZ\")"</span><span style="color: #0000FF;">}</span>
{x,y,z} = sort({x,y,z}) ?{x,y,z}
<span style="color: #0000FF;">?{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">}</span>
 
<span style="color: #0000FF;">{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">})</span> <span style="color: #0000FF;">?{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">}</span>
{x,y,z} = {77444,-12,0} ?{x,y,z}
{x,y,z} = sort({x,y,z}) ?{x,y,z}</lang>
<span style="color: #0000FF;">{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">77444</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">?{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">}</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">})</span> <span style="color: #0000FF;">?{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">}</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>{"lions, tigers, and","bears, oh my","(from the \"Wizard of OZ\")"}
Line 2,433 ⟶ 2,723:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
//Sort strings
$x = 'lions, tigers, and';
Line 2,468 ⟶ 2,758:
EOT;
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,482 ⟶ 2,772:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(let (X 77444 Y -12 Z 0)
(println X Y Z)
(mapc set '(X Y Z) (sort (list X Y Z)))
(println X Y Z) )</langsyntaxhighlight>
{{out}}
<pre>77444 -12 0
Line 2,492 ⟶ 2,782:
=={{header|Plain English}}==
This will sort integers only.
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Sort three numbers.
Line 2,515 ⟶ 2,805:
If the first number is greater than the second number, swap the first number with the second number.
If the second number is greater than the third number, swap the second number with the third number.
If the first number is greater than the second number, swap the first number with the second number.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,529 ⟶ 2,819:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">;sort three variables: x, y, z
 
;Macro handles any of the native types, including integers, floating point, and strings
Line 2,568 ⟶ 2,858:
Print(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
Sample output:
<pre>Sort three variables
Line 2,607 ⟶ 2,897:
===Python2===
This solution accepts 3 values either strings or numbers from user and then sort it in ascending order. This is implemented in Python 2.
<langsyntaxhighlight lang="python">
#python2 Code for Sorting 3 values
a= raw_input("Enter values one by one ..\n1.").strip()
Line 2,619 ⟶ 2,909:
b,c = c,b
print(str(a)+" "+str(b)+" "+str(c))
</syntaxhighlight>
</lang>
 
===Python3===
The following uses Python3.6 f-strings and uses the built in sorted function and assignment to a target list.
<langsyntaxhighlight lang="python">while True:
x, y, z = eval(input('Three Python values: '))
print(f'As read: x = {x!r}; y = {y!r}; z = {z!r}')
x, y, z = sorted((x, y, z))
print(f' Sorted: x = {x!r}; y = {y!r}; z = {z!r}')</langsyntaxhighlight>
 
{{out}}
Line 2,650 ⟶ 2,940:
(QACSFOT is the Quackery Arbitrary Character Sequence For Ordered Text, which is <code>0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz()[]{}<>~=+-*/^\|_.,:;?!'"`%@&#$</code>. Note that, unlike the even more arbitrary ASCII/Unicode character sequence, all the punctuation marks come after all the alphabetic characters, so the result of sorting the strings will vary from the result given in the task description.)
 
<langsyntaxhighlight Quackerylang="quackery"> [ stack ] is x
[ stack ] is y
[ stack ] is z
Line 2,678 ⟶ 2,968:
say " x = " x take echo cr
say " y = " y take echo cr
say " z = " z take echo cr</langsyntaxhighlight>
 
{{out}}
Line 2,694 ⟶ 2,984:
=={{header|R}}==
 
<syntaxhighlight lang="text">
#lang R
 
assignVec <- Vectorize("assign", c("x", "value"))
`%<<-%` <- function(x, value) invisible(assignVec(x, value, envir = .GlobalEnv)) # define multiple global assignments operator
</syntaxhighlight>
</lang>
 
{{out}}
 
<syntaxhighlight lang="text">
x <- 'lions, tigers, and'
y <- 'bears, oh my!'
Line 2,730 ⟶ 3,020:
z
## [1] 77444
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 2,736 ⟶ 3,026:
Ugh... mutation. Anyway...
 
<syntaxhighlight lang="text">#lang racket
 
(define-syntax-rule (sort-3! x y z <?)
Line 2,775 ⟶ 3,065:
(z '(from the "Wizard of OZ")))
(sort-3! x y z datum<?)
(for-each displayln (list x y z))))</langsyntaxhighlight>
 
{{out}}
Line 2,793 ⟶ 3,083:
The list (77444, -12, 0) is a poor choice to demonstrate numeric sort since it will sort the same numerically or lexically. Instead we'll use (7.7444e4, -12, 18/2). ( A Num, an Int, and a Rat. )
 
<syntaxhighlight lang="raku" perl6line># Sorting strings. Added a vertical bar between strings to make them discernable
my ($a, $b, $c) = 'lions, tigers, and', 'bears, oh my!', '(from "The Wizard of Oz")';
say "sorting: {($a, $b, $c).join('|')}";
Line 2,816 ⟶ 3,106:
# sort ALL THE THINGS
# sorts by lexical order with numeric values by magnitude.
.say for ($a, $b, $c, $x, $y, $z).sort;</langsyntaxhighlight>
 
{{out}}
Line 2,838 ⟶ 3,128:
=={{header|Red}}==
Red can natively sort values of various types. For instance here (not exhaustive): strings, integers, floats, characters, IP addresses, email addresses, words (~ variable names).
<langsyntaxhighlight Redlang="red">foreach [x y z] [
"lions, tigers, and"
"bears, oh my!"
Line 2,851 ⟶ 3,141:
set [x y z] sort reduce [x y z]
print ["x:" mold x "y:" mold y "z:" mold z]
]</langsyntaxhighlight>
{{out}}
<pre>x: {(from the "Wizard of OZ")} y: "bears, oh my!" z: "lions, tigers, and"
Line 2,867 ⟶ 3,157:
 
The literals can be of any length &nbsp; (only limited by virtual memory or language limitations).
<langsyntaxhighlight lang="rexx">/*REXX program sorts three (any value) variables (X, Y, and Z) into ascending order.*/
parse arg x y z . /*obtain the three variables from C.L. */
if x=='' | x=="," then x= 'lions, tigers, and' /*Not specified? Use the default*/
Line 2,881 ⟶ 3,171:
say '═════ sorted value of X: ' x
say '═════ sorted value of Y: ' y
say '═════ sorted value of Z: ' z</langsyntaxhighlight>
{{out|output|text=when using the default inputs:}}
<pre>
Line 2,897 ⟶ 3,187:
 
The maximum integer than be kept &nbsp; ''as an integer'' &nbsp; is &nbsp; (in this program) &nbsp; is 1,000 decimal digits.
<langsyntaxhighlight lang="rexx">/*REXX program sorts three (numeric) variables (X, Y, and Z) into ascending order. */
numeric digits 1000 /*handle some pretty gihugic integers. */ /*can be bigger.*/
parse arg x y z . /*obtain the three variables from C.L. */
Line 2,912 ⟶ 3,202:
y= low + mid + high - x - z /* " " middle " " " " " */ /* ◄─── sorting.*/
/*stick a fork in it, we're all done. */
say '═════ sorted values of X, Y, and Z: ' right(x, w) right(y, w) right(z, w)</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,920 ⟶ 3,210:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring"># Project : Sort three variables
 
x = 'lions, tigers, and'
Line 2,941 ⟶ 3,231:
see "z = " + str[3] + nl
see nl
</syntaxhighlight>
</lang>
Output:
<pre>x = (from the "Wizard of OZ")
Line 2,950 ⟶ 3,240:
y = 0
z = 77444</pre> =={{header|Ring}}==
<langsyntaxhighlight lang="ring">x = 77444
y = -12
z = 0
Line 2,966 ⟶ 3,256:
sList = sort(aList)
return sList
</syntaxhighlight>
</lang>
Output:
<pre>-12
Line 2,975 ⟶ 3,265:
bears, oh my!
lions, tigers, and</pre>
 
=={{header|RPL}}==
{{works with|HP|48}}
≪ → x y z
≪ x y z 3 →LIST SORT LIST→ DROP
≫ ≫ '<span style="color:blue">SORT3</span>' STO
 
"lions, tigers, and" "bears, oh my!" "(from the 'Wizard of OZ')" <span style="color:blue">SORT3</span>
77444 -12 9 <span style="color:blue">SORT3</span>
{{out}}
<pre>
6: "bears, oh my!"
5: "lions, tigers, and"
4: "(from the 'Wizard of OZ')"
3: -12
2: 9
1: 77444
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">x = 'lions, tigers, and'
y = 'bears, oh my!'
z = '(from the "Wizard of OZ")'
Line 2,983 ⟶ 3,291:
puts x, y, z
 
x, y, z = 7.7444e4, -12, 18/2r # Float, Integer, Rational; taken from Perl 6Raku
x, y, z = [x, y, z].sort
puts x, y, z
</syntaxhighlight>
</lang>
{{Out}}
<pre>(from the "Wizard of OZ")
Line 2,996 ⟶ 3,304:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn main() {
let mut array = [5, 1, 3];
array.sort();
Line 3,003 ⟶ 3,311:
println!("Reverse sorted: {:?}", array);
}
</syntaxhighlight>
</lang>
 
==Scala==
Line 3,010 ⟶ 3,318:
=={{header|Seed7}}==
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: genSort3 (in type: elemType) is func
Line 3,038 ⟶ 3,346:
doSort3(77444, -12, 0);
doSort3("lions, tigers, and", "bears, oh my!", "(from the \"Wizard of OZ\")");
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,050 ⟶ 3,358:
=={{header|SenseTalk}}==
 
<langsyntaxhighlight lang="sensetalk">Set x to "lions, tigers and"
Set y to "bears, oh my!"
Set z to "(from the Wizard of Oz)"
Line 3,089 ⟶ 3,397:
log d
 
LogSuccess "Sorted Numbers back into Original Variables"</langsyntaxhighlight>
 
=={{header|Sidef}}==
Generalized solution, for an arbitrary number of variable references:
<langsyntaxhighlight lang="ruby">func sort_refs(*arr) {
arr.map{ *_ }.sort ~Z arr -> each { *_[1] = _[0] }
}
Line 3,105 ⟶ 3,413:
say x
say y
say z</langsyntaxhighlight>
{{out}}
<pre>-12
Line 3,111 ⟶ 3,419:
77444</pre>
Alternatively, without using a sorting function:
<langsyntaxhighlight lang="ruby">var x = 77444
var y = -12
var z = 0
Line 3,121 ⟶ 3,429:
say x
say y
say z</langsyntaxhighlight>
{{out}}
<pre>-12
Line 3,129 ⟶ 3,437:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">func varSort<T: Comparable>(_ x: inout T, _ y: inout T, _ z: inout T) {
let res = [x, y, z].sorted()
 
Line 3,152 ⟶ 3,460:
print("x = \(x)")
print("y = \(y)")
print("z = \(z)")</langsyntaxhighlight>
 
{{out}}
Line 3,167 ⟶ 3,475:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">
set x {lions, tigers, and}
set y {bears, oh my!}
Line 3,188 ⟶ 3,496:
puts "y: $y"
puts "z: $z"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,198 ⟶ 3,506:
z: 77444
</pre>
 
=={{header|TXR}}==
 
The following is a comprehensive solution to the general problem of sorting any number of mutable places. We develop a macro operator called <code>sort-places</code> which is called with zero or more argument expressions that denote assignable places. The places are sorted in order according to the <code>greater</code> function.
 
The zero and one argument cases are handled as no-ops; the arguments are not evaluated at all, even for their side effects if they have any. The two argument case is handled by generating a conditional which controls a single swap. The three-argument case performs up to three swaps.
 
For four or more arguments, a hidden vector object is generated. The values of the places are stuffed into the vector, which is then sorted, after which the values are retrieved and stored in the places.
 
'''All cases work in such a way that the place expressions are evaluated at most once,''' which is achieved by leveraging the simple-to-use <code>placelet</code> macro.
 
<syntaxhighlight lang="txrlisp">(defmacro sort-places (. places)
(caseql (len places)
((0 1) nil)
(2 (with-gensyms (p0 p1)
^(placelet ((p0 (read-once ,[places 0]))
(p1 (read-once ,[places 1])))
(if (greater p0 p1)
(swap p0 p1)))))
(3 (with-gensyms (p0 p1 p2)
^(placelet ((p0 (read-once ,[places 0]))
(p1 (read-once ,[places 1]))
(p2 (read-once ,[places 2])))
(if (greater p0 p1)
(swap p0 p1))
(if (greater p1 p2)
(swap p1 p2))
(if (greater p0 p1)
(swap p0 p1)))))
(t (let ((gens [mapcar (ret (gensym)) places]))
(with-gensyms (vec)
^(placelet ,(zip gens places)
(let ((,vec (vec ,*gens)))
(nsort ,vec)
(set ,*(append-each ((g gens)
(i 0))
^(,g [,vec ,i]))))))))))
 
(prinl (sort-places))
 
(let ((x 1))
(sort-places x)
(prinl x))
 
(let ((x 2)
(y 1))
(sort-places x y)
(prinl (list x y)))
 
(let ((a 3)
(b 2)
(c 1))
(sort-places a b c)
(prinl (list a b c)))
 
(let ((a 4)
(b 3)
(c 2)
(d 1))
(sort-places a b c d)
(prinl (list a b c d)))</syntaxhighlight>
 
{{out}}
 
<pre>nil
1
(1 2)
(1 2 3)
(1 2 3 4)</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Sub Swap(Of T)(ByRef a As T, ByRef b As T)
Line 3,235 ⟶ 3,612:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>(-12, 0, 77444)
Line 3,243 ⟶ 3,620:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./sort" for Sort
import "./fmt" for Fmt
 
var sort3 = Fn.new { |x, y, z|
Line 3,271 ⟶ 3,648:
y = -9.7
z = 11.17
sort3.call(x, y, z)</langsyntaxhighlight>
 
{{out}}
Line 3,293 ⟶ 3,670:
=={{header|zkl}}==
This solution uses list assignment and list sorting. Lists are not homogeneous, but sorting usually expects that. If that is a problem, you can give the sort a compare function. Numbers (real and integer) are homogeneous enough to sort.
<langsyntaxhighlight lang="zkl">x,y,z := "lions, tigers, and", "bears, oh my!", 0'|(from the "Wizard of OZ")|;
x,y,z = List(x,y,z).sort();
println(x," | ",y," | ",z);
Line 3,299 ⟶ 3,676:
x,y,z := 77444, -12, 0;
x,y,z = List(x,y,z).sort();
println(x," ",y," ",z);</langsyntaxhighlight>
{{out}}
<pre>(from the "Wizard of OZ") | bears, oh my! | lions, tigers, and
490

edits