risortlib
This library sorts the items of a linked list, an array declared with
Dim List, into alphabetical order by a string field of the items. The
comparison ignores the case of letters. Optionally a long field of the
items can serve as a priority that is compared before the string. The
library needs utility.library, which it opens when the program starts;
without it the sorts do nothing and return 0. The commands work in
Amiga and Blitz mode.
The sort exchanges the contents of the items, not their links, so the list keeps its order of items and each item gets other contents. It is a simple method that compares every item with every following one: fast enough for most lists, but slow for long ones.
A list whose items hold a word and then a string, sorted by the string at offset 2:
NEWTYPE .listitem
pad.w
text$
End NEWTYPE
Dim List myitems.listitem(10)
If AddLast(myitems()) Then myitems()\text="World"
If AddLast(myitems()) Then myitems()\text="Hello"
StringSort myitems(),SizeOf .listitem,2
ResetList myitems()
While NextItem(myitems())
NPrint myitems()\text
WendThe fixed bugs of the library are listed in Fixes in risortlib.
Based on the RI Sort Lib guide by Reflective Images.
Library number 13.
Commands
ListBase
The address of the linked list list, unchanged; the list’s own data, with its pointers to the first and the current item, lies just in front of it.
Function, result .l. Help text: (list)
Arguments: array()
SortPriorityOffset
Makes the sorts compare a long priority field at offset in each item first, and the strings only for items of equal priority; items with a higher priority come first. A negative offset, such as -1, switches the priority off, which is the default.
Statement. Help text: offset -- -1 = disabled
Arguments: .w
StringSort
Sorts a whole linked list by the string at offset (0 without it) in each item; sizeof.type is the size of an item, SizeOf of its type. As a function it returns -1 when the list is sorted and 0 if it could not be sorted.
Statement or function, result .l. Help text: list,sizeof.type[,offset] - sort from start of list
2 forms:
array(), .warray(), .w, .w
It returns 0 if utility.library could not be opened, if the array is
no list, if sizeof.type is 0, or if the string field of an item
holds no string at all (the pointer is 0); in the last case the list
is left partly sorted.
StringSortDir
Sets the order of the sorts, 0 for ascending (A to Z), any other value for descending (Z to A).
Statement. Help text: direction
Arguments: .w
The direction applies to the strings only: with SortPriorityOffset,
the item with the higher priority comes first in both directions.
StringSortItem
Like StringSort, but sorts the list only from its current item on, so that a part of a list can be sorted. It also returns 0 if the list has no current item.
Statement or function, result .l. Help text: list,sizeof.type[,offset] - sort from current list item
2 forms:
array(), .warray(), .w, .w