7
Crystal Reports: ShellSort() Function
No comments · Posted by Craig Buchanan in Business Objects Enterprise, Crystal Reports, Functions, Programming
Sort an array of numbers using the Shell sort algorythm. This sorting algorythm will quickly sort arrays in Crystal Reports.
Function ShellSort(Values() As Number)
‘exit if array has no elements
If Ubound(Values)=0 Then Exit Function‘account for optional arguments
Dim LastElement As number
LastElement = UBound(Values)‘Crystal Reports’ arrays are 1-based.
Dim FirstElement As Number
FirstElement = 1Dim Elements As Number
Elements = LastElement – FirstElement + 1‘find the best value for distance
Dim distance As Number
Do
distance = distance * 3 + 1
Loop Until distance > Elements
Dim temp as number
Dim index As Number, index2 As Number
Do
distance = distance \ 3
For index = distance + FirstElement To LastElement
temp = Values(index)
index2 = indexDo While (Values(index2 – distance) > temp)
Values(index2) = Values(index2 – distance)
index2 = index2 – distance
If index2 – distance < FirstElement Then Exit Do
Loop
Values(index2) = temp
Next
Loop Until distance = 1
ShellSort=Values
End Function
Parameters
- Values
- An array of numbers.
Return Value
A sorted array of numbers
Remarks
Arrays in Crystal Reports are limit to 1000 elements.
Example
//create an array of numbers
Numbervar Array numbers:=[44,2,77,4,9,1,0,55,3];
//sort the array
numbers:=ShellSort(numbers);
//returns 0,1,2,3,4,9,44,55,77,
numbervar i;
stringvar t;
for i := 1 to ubound(numbers) do
t:=t & cstr(numbers[i],0) & “,”;
t;
Requirements
This function requires the use of Basic Syntax.
Business Objects · Crystal Reports · Functions · Programming
