Anonymous user
Sort three variables: Difference between revisions
Add source for Rust
(Added Wren) |
(Add source for Rust) |
||
Line 302:
=={{header|AutoHotkey}}==
<lang AutoHotkey>SortThreeVariables(ByRef x,ByRef y,ByRef z){
}</lang>
Examples:<lang AutoHotkey>x = lions, tigers, and
Line 345:
int main()
{
}
}
}
}
}
</lang>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.
Line 415:
int main()
{
}
</lang>
Line 820:
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. <lang Fortran> SUBROUTINE SORT3(X,Y,Z)
CHARACTER*(*) X,Y,Z
CHARACTER*(MAX(LEN(X),LEN(Y),LEN(Z))) T
IF (X.GT.Y) CALL SWAPC(X,Y)
IF (Y.GT.Z) CALL SWAPC(Y,Z)
IF (X.GT.Y) CALL SWAPC(X,Y)
CONTAINS
SUBROUTINE SWAPC(A,B)
CHARACTER*(*) A,B
T = A
A = B
B = T
END SUBROUTINE SWAPC
END SUBROUTINE SORT3
PROGRAM POKE
CHARACTER*28 XYZ(3,2)
DATA XYZ/
1 'lions, tigers, and','bears, oh my!','(from the "Wizard of OZ")',
2 '77444',' -12',' 0'/
INTEGER I
DO I = 1,2
WRITE (6,66) "Supplied: ", XYZ(1:3,I)
66 FORMAT (A12,3(" >",A,"<"))
CALL SORT3(XYZ(1,I),XYZ(2,I),XYZ(3,I))
WRITE (6,66) "Sorted, ? ", XYZ(1:3,I)
END DO
END
</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.
Line 1,713:
<pre># test ();;
case 1:
case 1:
- : unit = ()</pre>
Line 2,237:
9/1
77444.0</pre>
=={{header|Rust}}==
<lang rust>fn main() {
let mut array = [5, 1, 3];
array.sort();
println!("Sorted: {:?}", array);
array.sort_by(|a, b| b.cmp(a));
println!("Reverse sorted: {:?}", array);
}
</lang>
==Scala==
|