Task: Sort three variables



C#: using System;
public class Program
{
    public static void Main()
    {
        (int x, int y, int z) = (77444, -12, 0);

        
        if (x > y) (x, y) = (y, x);
        if (x > z) (x, z) = (z, x);
        if (y > z) (y, z) = (z, y);
        Console.WriteLine((x, y, z));

        var (a, b, c) = (
            "lions, tigers, and",
            "bears, oh my!",
            "(from the 'Wizard of OZ')");
        
        
        Sort(ref a, ref b, ref c);
        Console.WriteLine((a, b, c));
    }
    
    public static void Sort<T>(ref T a, ref T b, ref T c)
        where T : IComparable<T>
    {
        if (a.CompareTo(b) > 0) (a, b) = (b, a);
        if (a.CompareTo(c) > 0) (a, c) = (c, a);
        if (b.CompareTo(c) > 0) (b, c) = (c, b);
    }
}


Go: package main

import (
    "fmt"
    "log"
    "sort"
)

var (
    stringsIn = []string{
        `lions, tigers, and`,
        `bears, oh my!`,
        `(from the "Wizard of OZ")`}
    intsIn = []int{77444, -12, 0}
)

func main() {
    {
        
        x, y, z := stringsIn[0], stringsIn[1], stringsIn[2]

        
        
        s := []string{x, y, z}
        sort.Strings(s)
        x, y, z = s[0], s[1], s[2]

        
        if x > y || y > z {
            log.Fatal()
        }

        
        
        
        x, y, z = stringsIn[0], stringsIn[1], stringsIn[2] 
        if x < y {
            switch {
            case y < z:
            case x < z:
                y, z = z, y
            default:
                x, y, z = z, x, y
            }
        } else {
            switch {
            case x < z:
                x, y = y, x
            case z < y:
                x, z = z, x
            default:
                x, y, z = y, z, x
            }
        }
        if x > y || y > z { 
            log.Fatal()
        }

        
        
        x, y, z = stringsIn[0], stringsIn[1], stringsIn[2] 
        if x > y {
            x, y = y, x
        }
        if y > z {
            y, z = z, y
        }
        if x > y {
            x, y = y, x
        }
        if x > y || y > z { 
            log.Fatal()
        }
        fmt.Println("sorted strings:")
        fmt.Println(" ", x)
        fmt.Println(" ", y)
        fmt.Println(" ", z)
        fmt.Println("original data:")
        fmt.Println(" ", stringsIn[0])
        fmt.Println(" ", stringsIn[1])
        fmt.Println(" ", stringsIn[2])
    }
    
    {
        
        x, y, z := intsIn[0], intsIn[1], intsIn[2] 
        s := []int{x, y, z}
        sort.Ints(s)
        x, y, z = s[0], s[1], s[2]
        if x > y || y > z { 
            log.Fatal()
        }

        
        x, y, z = intsIn[0], intsIn[1], intsIn[2] 
        if x < y {
            switch {
            case y < z:
            case x < z:
                y, z = z, y
            default:
                x, y, z = z, x, y
            }
        } else {
            switch {
            case x < z:
                x, y = y, x
            case z < y:
                x, z = z, x
            default:
                x, y, z = y, z, x
            }
        }
        if x > y || y > z { 
            log.Fatal()
        }

        
        x, y, z = intsIn[0], intsIn[1], intsIn[2] 
        if x > y {
            x, y = y, x
        }
        if y > z {
            y, z = z, y
        }
        if x > y {
            x, y = y, x
        }
        if x > y || y > z { 
            log.Fatal()
        }
        fmt.Println("sorted ints:", x, y, z)
        fmt.Println("original data:", intsIn)
    }
    
    
    {
        sort3 := func(x, y, z int) (int, int, int) {
            if x > y {
                x, y = y, x
            }
            if y > z {
                y, z = z, y
            }
            if x > y {
                x, y = y, x
            }
            return x, y, z
        }
        x, y, z := intsIn[0], intsIn[1], intsIn[2] 
        x, y, z = sort3(x, y, z)
        if x > y || y > z { 
            log.Fatal()
        }
    }
    
    {
        sort3 := func(x, y, z *int) {
            if *x > *y {
                *x, *y = *y, *x
            }
            if *y > *z {
                *y, *z = *z, *y
            }
            if *x > *y {
                *x, *y = *y, *x
            }
        }
        x, y, z := intsIn[0], intsIn[1], intsIn[2] 
        sort3(&x, &y, &z)
        if x > y || y > z { 
            log.Fatal()
        }
    }
}