Searching and sorting arrays and lists
 

Searching lists and arrays for specific values has to be done manually - there are no special commands for doing this. However, to be able to search a list or array, you need to know about loops and other program structures, which are all described in the Program Flow topic. Specific examples will be included in that topic for real-world tasks related to searching for items in arrays and lists.

However, there are some special commands which you can use to sort arrays and lists. Some of these are provided in the original set of commands for Blitz 2 and some require you to use either the RISortLib (which is available as part of the RIBlitzLibs or Blitz Support Suite) or the NSortLib (part of the NewCommandSet). (You can get the Blitz Support Suite and NewCommandSet from the archives section of this site.)

Sorting arrays
There are sets of commands to sort arrays. The first is the Acid commands, which come with Blitz. There are three of these commands you can use to be able to sort arrays:

  • SortUp - Selects the order for sorting as ascending (sorts from lowest to highest, default mode)
  • SortDown - Selects the order for sorting as descending (sorts from highest to lowest)
  • Sort arrayname() - Performs the search on the array specified by arrayname. You can only sort arrays of the basic built-in types.
Code Dim foo.w(3) ; Create array which we will use for sorting
foo(0) = 34
foo(1) = 4
foo(2) = 23
foo(3) = 100
; Sort array from lowest to highest and display contents
SortUp ; Set sort direction
Sort foo() ; Perform the sort
NPrint foo(0) ; Display contents of the array
NPrint foo(1)
NPrint foo(2)
NPrint foo(3)
; Sort array from highest to lowest and display contents
SortDown ; Set sort direction
Sort foo() ; Perform the sort
NPrint foo(0) ; Display contents of the array
NPrint foo(1)
NPrint foo(2)
NPrint foo(3)
MouseWait
End
The problem with these commands is when you try to sort an array that you have not used all the items. For example, if the array in the above code was dimmed with 20 items and we filled the array items with indexes 0 to 10, the Sort command would still sort the items which we have not put anything in (indexes 11 to 20). You would therefore end up with an incorrect array after sorting. I'm not sure about the speed of the sort used, as I have never tested it and there is no mention in the docs of the algorithm used.

However, these disadvantages can be overcome by using the sort routines from the NewCommandSet. You can specify a range of indexes into the array to perform the sort between, and it uses the QuickSort algorithm, which is one of the fastest (especially when larger arrays are used). The NCS commands for sorting ar:

  • NSortDown arrayname(), start.w, end.w - sorts the array specified by arrayname into descending order (highest to lowest), starting from the index specified by start and ending at the index specified by end (so allowing you to sort only part of an array). NB, start and end must be valid numbers, that is between 0 and the number you dimmed your array with.
  • NSortUp arrayname(), start.w, end.w - sorts the array specified by arrayname into ascending order (lowest to highest), starting from the index specified by start and ending at the index specified by end (so allowing you to sort only part of an array). NB, start and end must be valid numbers, that is between 0 and the number you dimmed your array with.
Please note that the sort commands from the NCS can only be used to sort byte, word and longword arrays (although I suspect it should also work with quick arrays). It looks like the size of the arrays you can sort with these commands is also restricted to 32767 items (the maximum number you can fit in a word, for e.g. start or end).
Code Dim foo.w(3) ; Create array which we will use for sorting
foo(0) = 34
foo(1) = 4
foo(2) = 123
foo(3) = 10
; Sort part of the array from lowest to highest and display contents
NSortUp foo(),1,3 ; Perform the sort
NPrint foo(0) ; Display contents of the array
NPrint foo(1)
NPrint foo(2)
NPrint foo(3)
; Sort part of the array from highest to lowest and display contents
NSortDown foo(),0,2 ; Perform the sort
NPrint foo(0) ; Display contents of the array
NPrint foo(1)
NPrint foo(2)
NPrint foo(3)
MouseWait
End

Sorting Lists
There are again two methods for sorting lists, one being the Acid commands and the other being the RIBlitzLibs commands. The Acid command lets you sort only in ascending order (lowest to highest) AFAIK and only works on numeric values (for byte, word, longword and quick types - not float though). The RIBlitzLibs commands allow you sort in either direction and will only sort strings. Both sets of commands allow lists of these basic types to be sorted, and also lists of newtypes (newtypes are discussed in the next topic). The commands are:

  • SortList listname(),offset - The single Acid command. This one sorts the list of numbers called listname into ascending order. The offset parameter is used when you want to sort on a field inside a newtype - for plain lists of the basic types, just put zero in there.
  • StringSort listname(),type size,offset - Sorts an entire list (specified by listname). Type size is the size in bytes of each item in the list, and offset is the offset in bytes into the type (of each list item) to find the string which should be used for the sort. In plain lists of the basic string type, just put zero here. The sort is case sensitive (which means 'A' will be treated differently from 'a', see the ASCII table in the documents section for the order of characters).
  • StringSortDir direction - Specifies the direction of the sort for the StringSort and StringSortItem commands. Zero means to sort in ascending order (lowest to highest) and non-zero means to sort in descending order (highest to lower).
  • StringSortItem listname(),type size,offset - The same as the StringSort command except it only sorts part of the list, from the current item to the end of the list. Useful if you only want to sort part of a list (obviously).
Code ; An example of sorting a list of numeric values using
; the Acid command SortList
Dim List blah.w(3) ; Create list
AddItem blah() ; Add some items and store some values in there
blah() = 23
AddItem blah()
blah() = 4
AddItem blah()
blah() = 100
AddItem blah()
blah() = 36
SortList blah(),0 ; Perform the sort
FirstItem blah() ; Display the contents of the list in order
NPrint blah()
NextItem blah
NPrint blah()
NextItem blah
NPrint blah()
NextItem blah
NPrint blah()
MouseWait
End
Code ; An example of sorting a list of numeric values using
; the RIBlitzLibs commands
Dim List blah.s(3) ; Create list
AddItem blah() ; Add some items and store some values in there
blah() = "hello"
AddItem blah()
blah() = "Hello"
AddItem blah()
blah() = "HELLO"
AddItem blah()
blah() = "foo"
StringSortDir 1 ; Non-zero value here means to sort in descending order
StringSort blah(),4,0 ; Perform the sort (each item takes up 4 bytes - the basic part of a string requires 4 bytes, not including any memory the actual string requires)
FirstItem blah() ; Display the contents of the list in order
NPrint blah()
NextItem blah
NPrint blah()
NextItem blah
NPrint blah()
NextItem blah
NPrint blah()
MouseWait
End

These examples will make more sense, and will be better illustrated in the Program Flow topic, which will demonstrate using loops to go through arrays and lists.


Previous: Working with lists Programming contents Next


News | Introduction | Search | List/Forum/IRC Webring | Contact
Installation | Troubleshooting | Programming | Archives | Links
Return to Programming main
Page last modified: 12th September 2001