Funzione per Ordinare Una NotesDocumentCollection

TIPS DEVELOPERS

  • 2 commenti
Mentre stavo procedendo alla creazione della NEWSLETTER di Dominopoint.it, ho utilizzato una comodissima funzione che permette di ordinare i documenti contenuti all'interno di una NotesDocumentCollection generata da una PicklistCollection che come tutti i programmatori sanno non ritorna mai l'elenco dei documenti in manera ordinata...

La funzione vuole come parametro una NotesDocumentColletion e l'elenco dei nomi dei campi sui quali effettuare di volta in volta l'ordinamento...


Eccovi il Codice:
**********************************
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 is an array of strings with the fieldnames 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 = "(all)"                '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 = coll.Parent.GetDocumentByUNID(Right(arrSort(i), 32))
                Call collSorted.AddDocument(doc)
        Next i
        ' ---
        ' --- 4) Return collection
        ' ---
        Set SortCollection = collSorted
End Function
**********************************

2 Commenti:

  • #1 massimo 02/29/2008 11:15:54 AM

    Funziona...

    E' un algoritmo di ordinamento lessicografico: confronta stringhe.

    Se hai delle date, mi pare di capire, devi fare un lavoro di preparazione.

    Ho utilizzato l'algoritmo su moli di dati importanti e sono soddisfatto delle performance.

  • #2 marco 08/22/2007 3:58:08 PM

    Non funziona ....

    Ordina solo per i giorni e non per mese ed anno

Commenta articolo
 

Questo spazio web è stato creato da per un uso pubblico e gratuito. Qualsiasi tipo di collaborazione sarà ben accetta.
Per maggiori informazioni, scrivete a info@dominopoint.it

About Dominopoint
Social
Dominopoint social presence: