Sort three variables: Difference between revisions

→‎{{header|Java}}: New version that compiles without warnings.
(Found incorrectness.)
(→‎{{header|Java}}: New version that compiles without warnings.)
Line 911:
 
=={{header|Java}}==
{{incorrecttrans|lang|CompilingKotlin}} givesSlightly amodified. leastCompiles 6without warnings, sounder itJava smells10.}}
<lang Java>public class SortThreeVariables {
{{trans|Kotlin}}
private static class Triple<T extends Comparable<T>> {
<lang Java>import java.util.Arrays;
 
public class SortThreeVariables {
private static class Triple<T> {
T first, second, third;
 
Triple(T x, T y, T z) {
first = x;
second = y;
third = z;
}
Triple<T> sortInPlace()
{
T s;
if( first.compareTo( second ) > 0 ){ s = second; second = first; first = s; }
if( first.compareTo( third ) > 0 ){ s = third; third = first; first = s; }
if( second.compareTo( third ) > 0 ){ s = third; third = second; second = s; }
return this;
}
}
 
private static <T extends Comparable<T>> Triple<T> sortThree(T x, T y, T z) {
Comparable[]return a =( new Comparable[]{Triple<T>( x, y, z} ) ).sortInPlace();
Arrays.sort(a);
return new Triple<>((T) a[0], (T) a[1], (T) a[2]);
}
 
private static <T> void printThree(T x, T y, T z) {
System.out.printf("x=%s%ny=%s%nz=%s%n%n", x, y, z);
}
 
public static void main(String[] args) {
String x = "lions, and tigers, and";
Line 945 ⟶ 948:
z = t.third;
printThree(x, y, z);
 
Integer x2 = 77444;
Integer y2 = -12;
Line 954 ⟶ 957:
z2 = t2.third;
printThree(x2, y2, z2);
 
Double x3 = 174.5;
Double y3 = -62.5;
3,038

edits