Task: Sort three variables




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()
        }
    }
}





VB: Module Module1

    Sub Swap(Of T)(ByRef a As T, ByRef b As T)
        Dim c = a
        a = b
        b = c
    End Sub

    Sub Sort(Of T As IComparable(Of T))(ByRef a As T, ByRef b As T, ByRef c As T)
        If a.CompareTo(b) > 0 Then
            Swap(a, b)
        End If
        If a.CompareTo(c) > 0 Then
            Swap(a, c)
        End If
        If b.CompareTo(c) > 0 Then
            Swap(b, c)
        End If
    End Sub

    Sub Main()
        Dim x = 77444
        Dim y = -12
        Dim z = 0
        Sort(x, y, z)
        Console.WriteLine((x, y, z))

        Dim a = "lions, tigers, and"
        Dim b = "bears, oh my!"
        Dim c = "(from the 
        Sort(a, b, c)
        Console.WriteLine((a, b, c))
    End Sub

End Module