Date: 02-16-2022
Return to Index
created by gbSnippets
'PB Array statements have built-in capability to manipulate arrays
'Syntax (String Array):
'ARRAY SORT dArray([index]) [FOR count] [,FROM start TO end] [,COLLATE {UCASE | cstring}] [,TAGARRAY tarray()] [,{ASCEND | DESCEND}]
'All or some elements of an array can be sorted
'Ascending/Descending sort options
'Tag-along array supported - its elements are swapped in the same order as the sorted array
'By default, sorting is case-sensitive. Use COLLATE UCASE to get case-insensitive sort
Array Sort MyArray() 'sorts entire array, ascending (default), case-sensitive
Array Sort MyArray(), Descend 'sorts entire array, descending, case-sensitive
Array Sort MyArray(), Collate UCase 'sorts entire array, ascending (default), case-insensitive
Array Sort MyArray(), Collate UCase, Descend 'sorts entire array, descending, case-insensitive
Array Sort MyArray(5) 'sorts array from 5 to UBound(MyArray), ascending
Array Sort MyArray(5), Descend 'sorts array from 5 to UBound(MyArray), descending
Array Sort MyArray(5) For 10 'sorts elements 5-14, ascending
Array Sort MyArray(5), For 10 Descend 'sorts elements 5-14, descending
Array Sort MyArray() 'sorts entire array, ascending
Array Sort MyArray(), Descend 'sorts entire array, descending
Array Sort MyArray(), TAGARRAY MyTagArray() 'sorts entire array, ascending (default). MyTagArray() elements swapped same as MyArray()
Array Sort MyArray(), TAGARRAY MyTagArray(), Descend 'sorts entire array, descending. MyTagArray() elements swapped same as MyArray()
'Compilable Example: (Jose Includes)
'The first button simply sorts a string array. The second button sorts a string
'array in descending order and shows the results of a numeric tagarray as well.
'Note: In this example, both sorts are Case-Sensitive!
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Control Add Button, hDlg, 200,"Push", 50,40,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Dim MyArray(9) as String, tempA$, tempB$, tempC$,i as Long, MyTagArray(9) as Long
Array Assign MyTagArray() = 0,5,3,6,4,2,9,7,8,1
Array Assign MyArray() = "A","F","d","G","e","C","J","h","i","B"
tempA$ = Join$(MyArray(), " ")
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Array Sort MyArray()
tempB$ = Join$(MyArray(), " ")
For i = 0 to 9 : tempC$ = tempC$ + Str$(MyTagArray(i)) + " " : Next i
MsgBox "start:" + $crlf + tempA$ + $crlf + $crlf + "result:" + $crlf + tempB$
End If
If CB.Msg = %WM_Command AND CB.Ctl = 200 AND CB.Ctlmsg = %BN_Clicked Then
Array Sort MyArray(), TAGARRAY MyTagArray(), Descend 'sorts entire array, descending. MyTagArray() elements swapped same as MyArray()
tempB$ = Join$(MyArray(), " ")
For i = 0 to 9 : tempC$ = tempC$ + Str$(MyTagArray(i)) + " " : Next i
MsgBox "start:" + $crlf + tempA$ + $crlf + $crlf + "result:" + $crlf + tempB$ + $crlf + $crlf + "tagarray:" + $crlf + tempC$
End If
End Function
'gbs_00236
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm