#!/usr/bin/env ferite
uses "console", "array";

class fred
{
    string name;
    number num;

    function constructor(string name, number num)
    {
        .name = name;
	.num = num;
    }

    static function compare(object a, object b)
    {
        if(a.num < b.num) return -1;
	else if(a.num == b.num) return 0;
	else return 1;
    }

    function toString()
    {
        return "[ " + .num + " : " + .name + " ]";
    }
}

class barney {}

array a, b, c;
number n;
string s;
object o;

a = [ 7, 2, 11, 4, 3, 8 ];
Console.println("Unsorted ints:");
Array.each( a ) using ( n ){ Console.print("$n " ); };
Console.println("\nAscending sort:");
b = Array.sort(a, Array.SORT_ASCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\nDescending sort:");
b = Array.sort(a, Array.SORT_DESCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\n");

a = [ 93.6, 9.78, 73.3, 24.8, 9.63, 8.21 ];
Console.println("Unsorted floats:");
Array.each( a ) using ( n ){ Console.print("$n " ); };
Console.println("\nAscending sort:");
b = Array.sort(a, Array.SORT_ASCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\nDescending sort:");
b = Array.sort(a, Array.SORT_DESCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\n");

a = [ 4, 7, 12.3, 12, 13, 6.99, 18, 2.4 ];
Console.println("Unsorted mixed ints and floats:");
Array.each( a ) using ( n ){ Console.print("$n " ); };
Console.println("\nAscending sort:");
b = Array.sort(a, Array.SORT_ASCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\nDescending sort:");
b = Array.sort(a, Array.SORT_DESCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\n");

a = [ "wilma", "barney", "fred", "betty", "bambam", "dino", "pebbles" ];
Console.println("Unsorted strings:");
Array.each( a ) using ( n ){ Console.print("$n " ); };
Console.println("\nAscending sort:");
b = Array.sort(a, Array.SORT_ASCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\nDescending sort:");
b = Array.sort(a, Array.SORT_DESCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\n");

a = [ [ 1, 1, 1, 1 ], [ 2 ], [ 3, 3, 3 ], [ 4, 4 ] ];
Console.println("Unsorted arrays:");
Array.each( a ) using ( n ){ Console.print("$n " ); };
Console.println("\nAscending sort:");
b = Array.sort(a, Array.SORT_ASCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\nDescending sort:");
b = Array.sort(a, Array.SORT_DESCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\n");

a = [];
a[] = new fred("fred", 74);
a[] = new fred("wilma", 33);
a[] = new fred("barney", 62);
a[] = new fred("pebbles", 14);
a[] = new fred("bambam", 12);
a[] = new fred("betty", 4);
a[] = new fred("dino", 104);
Console.println("Unsorted objects:");
Array.each( a ) using ( n ){ Console.print("$n " ); };
Console.println("\nAscending sort:");
b = Array.sort(a, Array.SORT_ASCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\nDescending sort:");
b = Array.sort(a, Array.SORT_DESCENDING);
Array.each( b ) using ( n ){ Console.print("$n " ); };
Console.println("\n");
