QuickSort on document collection

Used to format a report document which returned selected documents and then sorted them by a specified sort field.

While many examples exist to sort strings, I didn't find many which sorted document collections. This was used to format a report document which returned selected documents and then sorted them by a specified sort field.


Sub SortQuick (aDocs() As NotesDocument, 
               sSort As String, 
               nParmLow As Integer, 
               nParmHigh As Integer)

%REM
SUMMARY  
Given an array of documents, perform a Quick Sort on the array.
The input parameters also include the sort field and the index range 
which is to be sorted.    The range is not assumed so that we can process 
multiple sort fields.  

HISTORY
Based on James Gosling's (jag@firstperson.com) Java algorithm.
%END REM
  
  Dim docCompare As NotesDocument  
  Dim docMid As NotesDocument
  Dim docHold As NotesDocument
  
  Dim nCalcLow As Integer
  Dim nCalcHigh As Integer
  Dim sSortFld As String
  
' If indices cross, done with this pass
  If (nParmHigh <= nParmLow) Then
    Exit Sub
  End If
  
' Load local copies of parameters
  sSortFld = sSort
  nCalcLow = nParmLow
  nCalcHigh = nParmHigh
  
' Get the midpoint document (partition element)
  Set docMid = aDocs((nCalcLow + nCalcHigh) / 2)
  
  Do While nCalcLow <= nCalcHigh

%REM
    Find the first element that is greater than or equal to
    the partition element starting from the left Index.
%END REM

    Set docCompare = aDocs(nCalcLow)
    Do While nCalcLow < nParmHigh And docCompare.GetItemValue(sSortFld)(0) < docMid.GetItemValue(sSortFld)(0)
      nCalcLow = nCalcLow + 1
      Set docCompare = aDocs(nCalcLow)      
    Loop
    
%REM
    Find the first element that is less than or equal to
    the partition element starting from the right Index.
%END REM    

    Set docCompare = aDocs(nCalcHigh)
    Do While nCalcHigh > nParmLow And docCompare.GetItemValue(sSortFld)(0) > docMid.GetItemValue(sSortFld)(0)
      nCalcHigh = nCalcHigh - 1
      Set docCompare = aDocs(nCalcHigh)
    Loop
    
%REM
    If the indices haven't crossed, swap the elements  
%END REM

    If (nCalcLow <= nCalcHigh) Then
      If (nCalcLow < nCalcHigh) Then
        Set docHold = aDocs(nCalcLow)
        Set aDocs(nCalcLow) = aDocs(nCalcHigh)
        Set aDocs(nCalcHigh) = docHold        
      End If
      nCalcLow = nCalcLow + 1
      nCalcHigh = nCalcHigh - 1      
    End If
  Loop
  
%REM
  if right index has not reached left side of array then recursively call the routine
  with the initial Left index and the new Right index to sort the left partition
%END REM

  If (nParmLow < nCalcHigh) Then
    Call SortQuick (aDocs(), sSortFld, nParmLow, nCalcHigh)  
  End If
  
%REM
  if left index has not reached right side of array then recursively call the routine
  with the new Left index and the inital Right index to sort the right partition
%END REM

  If (nCalcLow < nParmHigh) Then
    Call SortQuick (aDocs(), sSortFld, nCalcLow, nParmHigh)    
  End If
End Sub
This was first published in September 2001

Dig deeper on Lotus Notes Domino Administration Tools

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.UK

Close