Wave's~ BlitzMax Tutorial for NG~ November, 2015 ~ Version 11
Beginners guide to BlitzMax 

Sorting of Lists
To sort a list of strings or numbers in BMax is trival. To sort your own types such as Shipsinvolves nothing more complicated than too add and override a method in your type. Lists can also be copied, swapped, reversed and translated into arrays. You can also create a list from anarray.
 
Here are some methods:
' switch contents:
mylist.swap( myotherlist ) 'the first list's content moves to the second list
' while the second list's content gets moved to the first.

' reverse the order of the list:
mylist.reverse() 'the first is now the last.
newlist:tlist = MyList.Copy() 'creates a new exact copy of the list.
local array:object[] = MyList.ToArray() ' Converts the List to an Array of Objects.
Now to the sorting. To sort an array use the method sort(). It's the same as for arrays by the way, MyList.Sort. You can sort text like this. If you want to sort numbers then convert to an array or make objects out of them. You sort your own types by one or more fields, but to be able to sort ships by name you have to specify you want to sort them by the field called name.
Type TShip
  Field Name$,Score
  Global SortBy
  Const SortName = 1
  Const SortScore = 2
  Method Compare(O:Object) 'Override Original
    'Enter Compare Method Here
  End Method
EndType
'__________________________
Global Ship:TShip
Ship = New TShip
Ship.Name = "Sept"
Ship.Score = 11
 
Global Ship2:TShip
Ship2 = New TShip
Ship2.Name = "Madwell"
Ship2.Score = 3
 
Global Ship3:TShip
Ship3 = New TShip
Ship3.Name = "Townus"
Ship3.Score = 9
 
Global Ship4:TShip
Ship4 = New TShip
Ship4.Name = "Entus"
Ship4.Score = 4
'___________________________
Global MyList:TList 'Define the List
MyList = CreateList() 'Create a New List
MyList.AddLast(Ship) 'Add Ships to List
MyList.AddLast(Ship2)
MyList.AddLast(Ship3)
MyList.AddLast(Ship4)
MyList.Sort(True) 'False for Descending sort
For Local S:TShip = EachIn MyList
  Print "Name: "+S.Name+" Score:"+S.Score
Next
Print "Links/Objects in the List: "+MyList.Count() 'How many objects in the List?

That example won't be sorted. Not yet. First we have to complete the override method. That's the important part. Depending on how we do that will determine on how the sort will work.
 
First I'll start with a simple sort by score method:
Method Compare(Obj:Object) 'Override Original
  If TShip( Obj ).Score > Score Then
    Return
1
  Else
    Return
-1
  End If

End Method
First we see if the object is a Ship. Next we look to see which field is the highest, in this case score of the Ship calling compare (Self) or Score of the Ship we compare it too (called S in this example).
 
Method Compare(O:Object) 'Sort by Name
  If TShip(O).Name < Name Then
    Return
1
  Else
    Return
-1
  End If
End Method
This does the same but it checks the field name, which happens to be a string. No problems there. What if we want both?
 
Sort by a primary and a secondary field:
Method Compare(O:Object) 'Override Original
  If TShip(O).Score = Score Then
    If TShip(O).Name < Name Then
      Return
1
    Else
      Return
-1
    End If
  Else

    If TShip(O).Score > Score Then
      Return
1
    Else
      Return
-1
    End If
  End If

End Method
This sorts by Score, unless the score is a tie, then it will sort by name. You can of course continue this. If the names are equal sort by age and if age also is equal sort by something else. If you don't care who gets second at a tie just leave it.

To be able to sort a list in different ways you can specify a variable that holds the way to sort.
Method Compare(O:Object) 'Override Original
  If SortBy = SortName 'constant
    If TShip(O).Name < Name Then
      Return
1
    Else
      Return
-1
    End If
  ElseIf
SortBy = SortScore 'Constant
    If TShip(O).Score > Score Then
      Return
1
    Else
      Return
-1
    End If
  Else
'Don't sort
  End If
End Method

Now with one line before sort you can decide weather you want to sort by name or score:
 
TShip.Sortby = TShip.SortName
'OR
TShip.Sortby = TShip.SortScore
 
To Index | Next Page page 16