class Test {
static function main() {
// A simple Array of Ints
var array1:Array<Int> = [3, 5, 2, 1, 2, 3, 4];
// A not so simple Array of objects
var array2:Array<{i:Int, s:String}> = [
{i:3, s:"SECOND"},
{i:3, s:"THIRD"},
{i:1,s:"FIRST"},
{i:3, s:"FOURTH"}
];
trace("First array:");
trace("\t"+array1.join(", "));
trace("");
trace("Sort with array.sort()");
var copy = array1.copy();
copy.sort(function(a, b) {
if(a < b) return -1;
else if(a > b) return 1;
else return 0;
});
trace("\t"+copy.join(", "));
trace("");
trace("Sort with haxe.ds.ArraySort.sort()");
var copy = array1.copy();
haxe.ds.ArraySort.sort(copy, function(a, b) {
if(a < b) return -1;
else if(a > b) return 1;
else return 0;
});
trace("\t"+copy.join(", "));
trace("");
// a function to print the second array
function print(a:Array<{i:Int, s:String}>) {
trace("\t"+a.map(function(obj) return '${obj.s}').join(", "));
trace("");
}
trace("Second array:");
print(array2);
trace("Sort with array.sort()");
var copy = array2.copy();
copy.sort(function(a, b) {
if(a.i < b.i) return -1;
else if(a.i > b.i) return 1;
else return 0;
});
print(copy);
trace("Sort with haxe.ds.ArraySort.sort()");
var copy = array2.copy();
haxe.ds.ArraySort.sort(copy, function(a, b) {
if(a.i < b.i) return -1;
else if(a.i > b.i) return 1;
else return 0;
});
print(copy);
}
}