1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
//
// sort_array.ss
// Sorting arrays in SurgeScript
// Copyright 2017 Alexandre Martins <alemartf(at)gmail(dot)com>
//
object "Application"
{
arr = [3, 7, 1, 5, 9, 2, 4, 6, 8, 0];
state "main"
{
Console.print("Original array:");
printNumbers();
Console.print("Sorting in ascending order...");
arr.sort(null);
printNumbers();
Console.print("Sorting in descending order...");
arr.reverse();
printNumbers();
Console.print("Sorting in custom order (odds first)...");
arr.sort(spawn("SortStrategy.OddsFirst"));
printNumbers();
Application.exit();
}
fun printNumbers()
{
str = "";
for(i = 0; i < arr.length; i++)
str += arr[i] + " ";
Console.print(str);
}
}
// A custom sorting strategy that puts odd numbers first
object "SortStrategy.OddsFirst"
{
//
// The call() method compares two numbers.
//
// call(a, b) will return:
// -1 if a should come BEFORE b
// 1 if a should come AFTER b
// 0 if a and b share the same relative order
//
fun call(a, b)
{
aIsOdd = (Math.mod(a, 2) != 0);
bIsOdd = (Math.mod(b, 2) != 0);
if(aIsOdd && !bIsOdd)
return -1;
else if(!aIsOdd && bIsOdd)
return 1;
else if(a < b)
return -1;
else if(a > b)
return 1;
else
return 0;
}
}
|