Tip

Sorting Notes DocumentCollection by given field name

This tip was submitted to the SearchDomino.com 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  
          tempStore=collectDoc.GetItemValue(fieldName)
          If docInfo(0)="" Then
               docInfo(0)=collectDoc.UniversalID+"~"+tempStore(0)
          Else
               Redim Preserve docInfo(Ubound(docInfo)+1)
               docInfo(Ubound(docInfo))=collectDoc.UniversalID+"~"+tempStore(0)
          End If
          Set collectDoc=collect.GetNextDocument(collectDoc)
     Loop
     
     '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
                    tempStore=docInfo(y)
                    docInfo(y)=docInfo(x)
                    docInfo(x)=tempStore
               End If
          Next          
     Next
     
     'Get sorted document UNID array
     Redim sortedDocs(0)
     Forall z In docInfo
          If sortedDocs(0)="" Then
               sortedDocs(0)=GetDocUNID(z)
          Else
               Redim Preserve sortedDocs(Ubound(sortedDocs)+1)
               sortedDocs(Ubound(sortedDocs))=GetDocUNID(z)
          End If
     End Forall
     
     'Return final sorted value
     SortDocCollection=sortedDocs
     
End Function
Function GetFieldContent(source As Variant) As Variant     
     Dim pos As Integer
     '************************
     pos=Instr(source,"~")
     GetFieldContent=Right(source,Len(source)-pos)     
End Function
Function GetdocUNID(source As Variant) As Variant     
     Dim pos As Integer
     '************************
     pos=Instr(source,"~")
     GetdocUNID=Left(source,pos-1)     
End Function


MEMBER FEEDBACK TO THIS TIP

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)) + _
               
Right("0"+Cstr(Month(datetemp)),2)+Right("0"+Cstr(Day(datetemp)),2)
 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

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.