Make an instance of SortEngine using constructor methods (make, make_with ).
Change sorting if needed. The default is ascending.
For descending order, call set_ascending(false) of the instance.
For sorting one dimensional list, pass a list to sort_list as an argument.
The list passed as an argument is sorted. The returned list from sort_list is identical to the list passed as an argument.
useSortEngine : script "SortEngine"
(* sort a list *)
seta_listto {5, 7, 1, 9, 3, 4}
tell (makeSortEngine)
logsort_list(a_list)
--result :{1, 3, 4, 5, 7, 9}
seta_sortertoit
endtell
loga_list
-- result : {1, 3, 4, 5, 7, 9}
(* reverse sort *)
tella_sorter
set_ascending(false)
logsort_list(a_list)
--result : {9, 7, 5, 4, 3, 1}
endtell
Table Sort
SortEngine can sort every sub list in a two dimensional list with according to specified sub list. Use sort_table giving a two dimensional list and an index of a list which determines sorting order.
SortEngine in the default configuration can sort only values which can compared with AppleScript's comparison operators. To sort other values (e.g. list, record, script object and so on), give a script object (comparator script) to define magnitude relation between these values.
A comparator script must have following specifications
Must have do handler which have two positional arguments.
The do must return following values as a result of comparison of arguments.
1 : first argument > second argument
0 : first argument = second argument
-1 : first argument < second argument
A comparator script can be passed using constructor method make_with or instance method set_comparator into an instance.
The following sample is a script to sort lists which have two numerical elements. If the first element is same, the second element is considered to determine the sorting order.
SortEngine have tow sorting algorithm i.e. quick sort and bubble sort. In the default, quick sort is used.
The quick sort is faster algorithm than the bubble sort. But the quick sort is not stable i.e. equal values are exchange sometimes. On the other hand, the bubble sort can keep the order of equal values. You should properly choose sorting algorithms.