CustomSortArray()
Syntax
CustomSortArray(ArrayName(), @CompareProcedure() [, Options [, Start, End]])Description
Sorts the specified array, according to the given options using a custom procedure to compare array elements. The array may have a structure or one of basic type: byte, word, long, integer, string or float. Multi-dimensioned arrays are not supported.
Parameters
ArrayName() The array to sort. @CompareProcedure() The address of a procedure to compare array 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 array in ascending order (lower values first). This is the default. #PB_Sort_Descending: Sort the array in descending order (higher values first)Start, End (optional) The index of the first and last element in the array that should be sorted. If these parameters are not specified, then the whole array is sorted.
Return value
None.
Example: Sort structured array
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 Dim Test.CustomStruct(6) Test(0)\Value$ = "0005" Test(1)\Value$ = "2" Test(2)\Value$ = "42" Test(3)\Value$ = "7" Test(4)\Value$ = "23" Test(5)\Value$ = "100" Test(6)\Value$ = "101" CustomSortArray(Test(), @CustomCompare(), #PB_Sort_Ascending) For i = 0 To 6 Debug Test(i)\Value$ Next
Example: Sort basic type array
; 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 Dim Test.f(4) Test(0) = 2.5 Test(1) = -3.0 Test(2) = 0 Test(3) = 12 Test(4) = -100 CustomSortArray(Test(), @AbsCompare()) For i = 0 To 4 Debug Test(i) Next
See Also
SortArray(), SortStructuredArray(), RandomizeArray()
Supported OS
All