Synopsis|SortEngine Reference

Usage

Basic Usage

  1. Make an instance of SortEngine using constructor methods (make, make_with ).
  2. Change sorting if needed. The default is ascending.
    • For descending order, call set_ascending(false) of the instance.
  3. For sorting one dimensional list, pass a list to sort_list as an argument.
  4. The list passed as an argument is sorted. The returned list from sort_list is identical to the list passed as an argument.
use SortEngine : script "SortEngine"

(* sort a list *)
set a_list to {5, 7, 1, 9, 3, 4}
tell (make SortEngine)
log sort_list(a_list)
--result :{1, 3, 4, 5, 7, 9}
set a_sorter to it
end tell
log a_list
-- result : {1, 3, 4, 5, 7, 9}

(* reverse sort *)
tell a_sorter
set_ascending(false)
log sort_list(a_list)
--result : {9, 7, 5, 4, 3, 1}
end tell

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.

use SortEngine : script "SortEngine"

set a_list2d to {{"a", "b", "c"}, {"d", "e", "f"}, {3, 2, 1}}

tell (make SortEngine)
sort_table(a_list2d, -1)
--result : {{"c", "b", "a"}, {"f", "e", "d"}, {1, 2, 3}}
end tell

Custom Comparator

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

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.

use SortEngine : script "SortEngine"

script ListComparator
property parent : SortEngine's base_comparator()
property _compare_order : {1, 2}

on do(a_first, a_second)
repeat with ith from 1 to length of my _compare_order
set an_index to item ith of my _compare_order
set a_result to continue do(item an_index of a_first, item an_index of a_second)
if a_result is not 0 then
exit repeat
end if
end repeat

return a_result
end do
end script

set a_list to {{2, 2}, {1, 2}, {1, 1}, {2, 1}}
tell SortEngine's make_with({comparator:ListComparator})
sort_list(a_list)
--result :{{1, 1}, {1, 2}, {2, 1}, {2, 2}}
end tell

Quick Sort vs Bubble Sort

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.

use SortEngine : script "SortEngine"

(* quick sort (default) *)
set a_list to {{"c", "a", "b", "a", "c", "b", "a"}, {1, 1, 1, 2, 2, 2, 3}}
tell SortEngine's make_with({sorter:"quick"})
log sort_table(a_list, 1)
-- result :{{a, a, a, b, b, c, c}, {3, 1, 2, 1, 2, 2, 1}}
end tell

(* bubble sort *)
set a_list to {{"c", "a", "b", "a", "c", "b", "a"}, {1, 1, 1, 2, 2, 2, 3}}
tell SortEngine's make_with({sorter:"bubble"})
log sort_table(a_list, 1)
-- result : {{a, a, a, b, b, c, c}, {1, 2, 3, 1, 2, 1, 2}}
end tell
Synopsis|SortEngine Reference