QW Scripting: Sortlist
Sortlist
Sortlist is a subroutine that sorts a list of simple elements. Simple
elements are the built-in datatypes of QSL. For example numbers or strings.
The script uses a heapsort algorithm, which should have reasonably good
performance, even for quite large lists.
Information on sortlist is organized as follows:
There are two parameters to the sortlist subroutine:
- list: This is a required parameter. It is a standard
QSL list with one or more elements. The original list passed into
sortlist is never changed (see the result below).
- ascending: This is an optional parameter.
Pass true for ascending sort sequence and false for descending.
If the parameter is
omitted, ascending is assumed.
The return value of sortlist is the original list in sorted order.
The following provides an example of sorting a short list of numbers:
list = {5, 9, 1, 8, 2};
result = dialog("Before " + string(list));
sortedlist = sortlist(list, true);
result = dialog("After " + string(sortedlist));
The following provides an example of sorting a short list of strings.
In this case each string is only a single character long. We also ask
the the resulting list be in descending sequence:
list = {"f", "b", "a", "e", "d"};
result = dialog("Before " + string(list));
sortedlist = sortlist(list, false);
result = dialog("After " + string(sortedlist));
To test the performance of sortlist we created a test subroutine.
This subroutine creates the list in ascending sequence and then requests
that the list be sorted in descending sequence.
sub performancetest(count)
list = {};
repeat for inx from 1 to count
list = list + inx;
endrepeat
result = dialog("list ready to sort");
list = sortlist(list, false);
result = dialog("list sorted");
finished = false;
inx = 1;
repeat while not finished
if list[inx] < list[inx + 1] then
msg = "List not sorted at ";
msg = msg + "list[";
msg = msg + string(inx);
msg = msg + "] = ";
msg = msg + string(list[inx]);
msg = msg + " list[";
msg = msg + string(inx + 1);
msg = msg + "] = ";
msg = msg + string(list[inx + 1]);
result = dialog(msg);
finished = true;
else
inx = inx + 1;
if inx >= count then
finished = true;
endif
endif
endrepeat
endsub
performancetest(250);
The timing results were calculated from when the sort started
until when the sort ended. The performance results
using a preliminary retail build of QW 4.7.06 were as follows:
| Date:
| February 22, 1999 at 5:55 PM (Pacific)
|
| Person:
| David
|
| Machine:
| Vectra XU
|
| CPU:
| Dual Pentium 133
|
| Memory:
| 128 MB
|
| OS:
| WinNT 4.0 SP3
|
| Timing:
| 18 elapsed seconds for 250 entries
|
| Date:
| February 22, 1999 at 8:00 PM (Mountain)
|
| Person:
| Neil
|
| Machine:
| Dell Dimension xps D300
|
| CPU:
| Pentium II 300 Mhz
|
| Memory:
| 64 MB
|
| OS:
| WinNT 4.0 SP3
|
| Timing:
| 6 elapsed seconds for 250 entries
|