The key difference between GetAllEntriesbyKey and GetAllDocumentsbyKey

Is there any difference between collection returned by GetAllEntriesByKey and GetAllDocumentsByKey? yes. The NotesViewEntryCollection returns the sorted collection as it is sorted in notesview but NotesDocumentCollection doesn’t come with sorted order. The GetAllEntriesByKey runs faster compared to GetAllDocumentsByKey. Say for example, you wanted to show a list of documents in alphabet order or sorted by date. When using GetAllDocumentsByKey method, we need to write separate sorting (quick sort or bubble sort) functionality to sort the collection. We dont need to write sorting functionality if we use GetAllEntriesByKey method. because the returned entries collections are sorted already as it is presented in the view.

This will be very handy and helps to reduce the execution time when we need to work with more number of documents. Also it helps to reduce the number of lines in code.

Lotus Script Sorting document collection

Have a look at the script block below.
GetAllEntriesBykey returns the entry

1. GetAllEntriesByKey
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim vc As NotesViewEntryCollection
Set db = session.CurrentDatabase
Set view = db.GetView("By Category")
Set vc = view.GetAllEntriesByKey("Spanish leather", False)
set doc= vc.getfirstentry.document
2.GetAllDocumentsByKey
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim dc As NotesDocumentCollection
Dim dcSorted As NotesDocumentCollection
Set db = session.CurrentDatabase
Set view = db.GetView( "By Category" )
Set dc = view.GetAllDocumentsByKey("Spanish leather", False)
If dc.IsSorted Then
  Messagebox( "This collection is sorted" )
Else
  dc( "This collection is unsorted" )
End If
set dcSorted  = SortQuick (dc, "Category", 1, dc.count)
Based on Quick Sort script Link
Function 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 Function
  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

Leave a Reply

Your email address will not be published. Required fields are marked *