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








Ada: with Ada.Text_IO;
with Ada.Strings.Unbounded;

procedure Sort_Three is

   generic
      type Element_Type is private;
      with function "<" (Left, Right : in Element_Type) return Boolean;
   procedure Generic_Sort (X, Y, Z : in out Element_Type);

   procedure Generic_Sort (X, Y, Z : in out Element_Type)
   is
      procedure Swap (Left, Right : in out Element_Type) is
         T : constant Element_Type := Left;
      begin
         Left := Right;
         Right := T;
      end Swap;
   begin
      if Y < X then Swap (X, Y); end if;
      if Z < Y then Swap (Y, Z); end if;
      if Y < X then Swap (X, Y); end if;
   end Generic_Sort;

   procedure Test_Unbounded_Sort is
      use Ada.Text_IO;
      use Ada.Strings.Unbounded;

      X : Unbounded_String := To_Unbounded_String ("lions, tigers, and");
      Y : Unbounded_String := To_Unbounded_String ("bears, oh my!");
      Z : Unbounded_String := To_Unbounded_String ("(from the ""Wizard of OZ"")");

      procedure Sort is
         new Generic_Sort (Unbounded_String, "<");
   begin
      Sort (X, Y, Z);
      Put_Line (To_String (X));
      Put_Line (To_String (Y));
      Put_Line (To_String (Z));
      New_Line;
   End Test_Unbounded_Sort;

   procedure Test_Integer_Sort is

      use Ada.Text_IO;

      procedure Sort is
         new Generic_Sort (Integer, "<");

      X : Integer := 77444;
      Y : Integer :=   -12;
      Z : Integer :=     0;
   begin
      Sort (X, Y, Z);
      Put_Line (X'Image);
      Put_Line (Y'Image);
      Put_Line (Z'Image);
      New_Line;
   end Test_Integer_Sort;

begin
   Test_Unbounded_Sort;
   Test_Integer_Sort;
end Sort_Three;