CustomSortList()
Syntax
CustomSortList(ListName(), @CompareProcedure() [, Options [, Start, End]])Description
Sorts the specified list, according to the given options using a custom procedure to compare list elements. The list may have a structure or one of basic type: byte, word, long, integer, string or float.
Parameters
ListName() The list to sort. @CompareProcedure() The address of a procedure to compare list elements. The procedure must have two parameters that will receive the memory address of the two elements to compare and return one of the following values: #PB_Sort_Greater: The first element is greater than the second one. #PB_Sort_Equal : Both elements are equal. #PB_Sort_Lesser : The first element is lesser than the second one.The compare procedure is called very often during the sorting process so it should do as little work as possible and be fast to execute.Options (optional) It can be a combination of the following values: #PB_Sort_Ascending : Sort the list in ascending order (lower values first). This is the default. #PB_Sort_Descending: Sort the list in descending order (higher values first)Start, End (optional) The index of the first and last element in the list that should be sorted. If these parameters are not specified, then the whole list is sorted.
The first list element is at position 0, the next at 1 and so on.
Return value
None.
Example: Sort structured list
Structure CustomStruct Value$ EndStructure ; Sort by numeric value in the string element in the structure Procedure CustomCompare(*a.CustomStruct, *b.CustomStruct) If Val(*a\Value$) < Val(*b\Value$) ProcedureReturn #PB_Sort_Lesser ElseIf Val(*a\Value$) > Val(*b\Value$) ProcedureReturn #PB_Sort_Greater Else ProcedureReturn #PB_Sort_Equal EndIf EndProcedure NewList Test.CustomStruct() AddElement(Test()) : Test()\Value$ = "0005" AddElement(Test()) : Test()\Value$ = "2" AddElement(Test()) : Test()\Value$ = "42" AddElement(Test()) : Test()\Value$ = "7" AddElement(Test()) : Test()\Value$ = "23" AddElement(Test()) : Test()\Value$ = "100" AddElement(Test()) : Test()\Value$ = "101" CustomSortList(Test(), @CustomCompare(), #PB_Sort_Ascending) ForEach Test() Debug Test()\Value$ Next
Example: Sort basic type list
; Sort float elements by absolute value (ignoring the sign) Procedure AbsCompare(*a.Float, *b.Float) If Abs(*a\f) < Abs(*b\f) ProcedureReturn #PB_Sort_Lesser ElseIf Abs(*a\f) > Abs(*b\f) ProcedureReturn #PB_Sort_Greater Else ProcedureReturn #PB_Sort_Equal EndIf EndProcedure NewList Test.f() AddElement(Test()) : Test() = 2.5 AddElement(Test()) : Test() = -3.0 AddElement(Test()) : Test() = 0 AddElement(Test()) : Test() = 12 AddElement(Test()) : Test() = -100 CustomSortList(Test(), @AbsCompare()) ForEach Test() Debug Test() Next
See Also
SortList(), SortStructuredList(), RandomizeList()
Supported OS
All