Tip

QuickSort on document collection

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.