Sorting Notes DocumentCollection by given field name

Notes DocumentCollection is great, but the problem comes when you need to sort document collection by a particular field.

This tip was submitted to the tip exchange by member Manvendra Singh Rana. Please let others know

how useful it is via the rating scale at the end of the tip. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

View member feedback to this tip.

I am sure all experienced LN developers must have worked on Notes DocumentCollection. It's great to work with but problem comes when you need to sort document collection by a particular field name. Here is a piece of code which does that. It accepts notesdocumentcollection and field names as two parameters and returns an array of document UNIDs. You just need to run a loop and access documents (GetDocumentByUNID method in NotesDatabase class). For better usage I would recommend to use it as a script library.

Function SortDocCollection(collect As NotesDocumentCollection,fieldName As Variant) As Variant               
     Dim collectDoc As NotesDocument
     Dim docInfo As Variant
     Dim sortedDocs As Variant     
     Dim tempStore As Variant
     'Check proper function usage
     If collect.Count=0 Then
          Msgbox "Document collection does not contain any document.",16,"Execution Terminated"
          Exit Function
     End If
     'Get required document collection infromation for sorting
     Redim docInfo(0)
     Set collectDoc=collect.GetFirstDocument
     Do Until collectDoc Is Nothing  
          If docInfo(0)="" Then
               Redim Preserve docInfo(Ubound(docInfo)+1)
          End If
          Set collectDoc=collect.GetNextDocument(collectDoc)
     'Sort document collection
     For x=0 To Ubound(docInfo)
          For y=0 To Ubound(docInfo)
               If Strcompare(GetFieldContent(docInfo(x)),GetFieldContent(docInfo(y)),5)=-1 Then
               End If
     'Get sorted document UNID array
     Redim sortedDocs(0)
     Forall z In docInfo
          If sortedDocs(0)="" Then
               Redim Preserve sortedDocs(Ubound(sortedDocs)+1)
          End If
     End Forall
     'Return final sorted value
End Function
Function GetFieldContent(source As Variant) As Variant     
     Dim pos As Integer
End Function
Function GetdocUNID(source As Variant) As Variant     
     Dim pos As Integer
End Function


This is a wonderful piece. It would be better if it had the facility to supply ascending/descending as a parameter to the function. Is this possible?

—Krishnakumar S.


Yes, the user can sort collection in ascending/descending order by slightly tweaking the provided code.

  1. The function should accept one more argument:
    Function SortDocCollection(collect As NotesDocumentCollection,fieldName As 
    Variant, sortType as variant)

    The last argument sortType can be either -1 or 1 (This is important, sortType must not have any other value).

  2. A small change needs be made in the following line:
    If Strcompare(GetFieldContent(docInfo(x)),GetFieldContent(docInfo(y)),5)
    =sortType Then

By following this simple two step process the user should be able to control/decide the sorting order.

—Manvendra R.


This is just a quick, small addition to this tip that will also allow it to sort by a date field. Include the following code at the start of the first do until.

 Do Until collectDoc Is Nothing   'Current Code
  tempStore=collectDoc.GetItemValue(fieldName)  'Current 
Code 'New code
         If Isdate(tempstore(0)) Then
  Set dateTmp = New NotesDateTime(tempstore(0) )
  datetemp = datetmp.DateOnly
  tempstore(0) = Cstr(Year(datetemp)) + _
 End If
'/end new code

Note: Don't forget to dim the new variables datetmp and datetemp.

—George S.

Do you have comments of your own? Let us know.

This was first published in February 2002

Dig deeper on Domino Resources - Part 4



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:



  • iSeries tutorials'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 ...