Date: 02-16-2022
Return to Index
created by gbSnippets
'The built-in PowerBASIC Array Scan is capable of sorting all or part of
'an array (both numeric and string). It also supports the use of a Tag array,
'which can be of a different Type than the main array.
'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
'Primary Code:
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 3 'sorts elements 5-7, ascending
Array Sort MyArray(5) For 3, Descend 'sorts elements 5-7, 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)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord, MyArray() as String, MyTagArray() as Long
Function PBMain() As Long
Dialog New Pixels, 0, "Array Insert Test Code",300,300,270,400, %WS_OverlappedWindow To hDlg
Control Add Label, hDlg, 100, "Array Values: A G k M D r z p T", 10,10, 270,20
Control Add Label, hDlg, 150, "TagArray Values: 58 50 51 56 53 57 55 52 54", 10,40,270,20
Control Add Button, hDlg, 200, "Entire array, ascending", 10,70,240,25
Control Add Button, hDlg, 300, "Entire array, descending", 10,100,240,25
Control Add Button, hDlg, 320, "Entire array, ascending, case-insensitive", 10,130,240,25
Control Add Button, hDlg, 330, "Entire array, descending, case-insensitive", 10,160,240,25
Control Add Button, hDlg, 400, "Start at 5, rest of array, ascending", 10,190,240,25
Control Add Button, hDlg, 500, "Start as 5, rest of array, descending", 10,220,240,25
Control Add Button, hDlg, 600, "Start at 5, elements 5-7, ascending", 10,250,240,25
Control Add Button, hDlg, 700, "Start at 5, elements 5-7, descending", 10,280,240,25
Control Add Button, hDlg, 800, "Entire array, ascending, with TAG aarray", 10,310,240,25
Control Add Button, hDlg, 900, "Entire array, ascending, with TAG array", 10,340,240,25
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local iResult&, i as Long, temp$
If CB.Msg = %WM_Command AND CB.Ctlmsg = %BN_Clicked Then
ReDim MyArray(8), MyTagArray(8)
Array Assign MyArray() = "A","G","k","M","D","r","z","p","T"
Array Assign MyTagArray() = 58,50,51,56,53,57,55,52,54
Select Case CB.Ctl
Case 200
Array Sort MyArray() 'sorts entire array, ascending (default)
Case 300
Array Sort MyArray(), Descend 'sorts entire array, descending
Case 320
Array Sort MyArray(), Collate UCase 'sorts entire array, ascending (default), case-insensitive
Case 330
Array Sort MyArray(), Collate UCase, Descend 'sorts entire array, descending, case-insensitive
Case 400
Array Sort MyArray(5) 'sorts array from 5 to UBound(MyArray), ascending
Case 500
Array Sort MyArray(5), Descend 'sorts array from 5 to UBound(MyArray), descending
Case 600
Array Sort MyArray(5) For 3 'sorts elements 5-7, ascending
Case 700
Array Sort MyArray(5) For 3 ,Descend 'sorts elements 5-7, descending
Case 800
Array Sort MyArray(), TAGARRAY MyTagArray() 'sorts entire array, ascending (default). MyTagArray() elements swapped same as MyArray()
Case 900
Array Sort MyArray(), TAGARRAY MyTagArray(), Descend 'sorts entire array, descending. MyTagArray() elements swapped same as MyArray()
End Select
temp$ = "String Array now contains: " + $crlf + $crlf + myarray(0)
For i = 1 To 8 : temp$ = temp$ + " - " + myarray(i) : Next i
temp$ = temp$ + $crlf + $crlf + "Numeric Tag Array now contains:" + $crlf + $crlf + str$(mytagarray(0))
For i = 1 To 8 : temp$ = temp$ + " - " + str$(mytagarray(i)) : Next i
MsgBox temp$
End If
End Function
'gbs_00075
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm