Thursday 10 March 2011

Sort a notessdocumentcollection

Here is the code

Function SortCollection(coll As NotesDocumentCollection, fieldnames() As String) As NotesDocumentCollection ' ———————————————— ' — You may use and/or change this code freely ' — provided you keep this message ' — ' — Description: ' — Sorts and returns a NotesDocumentCollection ' — Fieldnames parameter is an array of strings ' — with the field names to be sorted on ' — ' — By Max Flodén 2005 - http://www.tjitjing.com ' ———————————————— Dim session As New NotesSession Dim db As NotesDatabase Dim collSorted As NotesDocumentCollection Dim doc As NotesDocument Dim i As Integer, n As Integer Dim arrFieldValueLength() As Long Dim arrSort, strSort As String Dim viewname As String, fakesearchstring As String viewname = "VMILESTONES" 'This could be any existing view in database with first column sorted fakesearchstring = "zzzzzzz" 'This search string must NOT match anything in view Set db = session.CurrentDatabase '— ' — 1) Build array to be sorted '— 'Fill array with fieldvalues and docid and get max field length Redim arrSort(0 To coll.Count -1, 0 To Ubound(fieldnames) + 1) Redim arrFieldValueLength(0 To Ubound(fieldnames) + 1) For i = 0 To coll.Count - 1 Set doc = coll.GetNthDocument(i + 1) For n = 0 To Ubound(fieldnames) + 1 If n = Ubound(fieldnames) + 1 Then arrSort(i,n) = doc.UniversalID arrFieldValueLength(n) = 32 Else arrSort(i,n) = “” & doc.GetItemValue(fieldnames(n))(0) ' Check length of field value If Len(arrSort(i,n)) > arrFieldValueLength(n) Then arrFieldValueLength(n) = Len(arrSort(i,n)) End If End If Next n Next i 'Merge fields into list that can be used for sorting using @Sort function For i = 0 To coll.Count - 1 If Not strSort = "" Then strSort = strSort & ":" strSort = strSort + "'" For n = Lbound(fieldnames) To Ubound(fieldnames) + 1 strSort = strSort & Left(arrSort(i,n) & Space(arrFieldValueLength(n)), arrFieldValueLength(n)) Next n strSort = strSort + "'" Next i ' — ' — 2) Sort array ' — arrSort = Evaluate(|@Sort(| & strSort & |)|) ' — ' — 3) Use sorted array to sort collection ' — Set collSorted = coll.Parent.GetView(viewname).GetAllDocumentsByKey(fakesearchstring) For i = 0 To Ubound(arrSort) Set doc = db.GetDocumentByUNID(Right(arrSort(i), 32)) Call collSorted.AddDocument(doc) Next i ' — ' — 4) Return collection ' — Set SortCollection = collSorted End Function Call function with: Dim strF(1) As String strF(0) = "Tab" strF(1) = "SequenseNr" Set dc = SortCollection(dc, strF )

No comments:

Post a Comment