Restored from previous forum. Originally posted by horst.
I think a sort function can be very simple, if the actual compare
operation is an external function written by the programmer.
This means, the programmer can write any code he wants to decide
whether two given elements are in the right order or not.
The user defined function may handle any type of variable, and do
any kind of conversions. Since the original elements with the original
structure are passed as parameters, the procedure will be transparent,
and in most cases trivial, like this:
Code: Select all
Procedure ByNameAscending(*a.test,*b.test)
If *a\name <= *b\name : result = #true : EndIf
ProcedureReturn result
EndProcedure
Or sorting by Name AND City, for example:
Code: Select all
Procedure ByNameAndCity(*a.test,*b.test)
If *a\name < *b\name or (*a\name = *b\name and *a\city <= *b\city)
ProcedureReturn #true
EndIf
EndProcedure
The procedure to apply is passed to the sort by pointer, for example:
SortList(LinkedList(),@ByNameAscending())
No other parameters are necessary.
Demo (with bubble sort):
http://home.mnet-online.de/horst.muc/pb/llsort.zip
Horst