Manage Learn to apply best practices and optimize your operations.

Sort collection like a view

How to sort a collection the same way a view is sorted.

View member feedback to this tip.

Ever needed to sort a collection the same way a view is sorted, like when exporting data or printing labels from selected documents? Well, here you go!

Function SortDocumentCollection 
(coll As NotesDocumentCollection, 
ViewName As
String) As NotesDocumentCollection 'This function takes a NotesdocumentCollection and sorts in the same order as the specified view. 'It actually creates a new collection by navigating through the view and adding documents 'as it encounters them in the NotesViewEntryCollection Dim session As New NotesSession Dim db As NotesDatabase Dim view As NotesView Dim entry As NotesViewEntry Dim vc As NotesViewEntryCollection Dim SortedColl As NotesDocumentCollection Dim doc As NotesDocument Set db = session.CurrentDatabase Set view = db.GetView(ViewName) Set vc = view.AllEntries 'initialise the new collection - trick is to create an empty collection because the collection needs to be initialized before we can add documents into it Set SortedColl = view. GetAllDocumentsByKey("zzxxzz") 'scan AllEntries Set entry = vc.GetFirstEntry While Not entry Is Nothing 'get a document from AllEntries and add it to the new collection Set doc = coll.GetDocument(entry.Document) Call SortedColl.AddDocument(doc) Set entry = vc.GetNextEntry(entry) Wend 'return the new collection Set SortDocumentCollection = SortedColl End Function

MEMBER FEEDBACK TO THIS TIP

I have a little note to add: I discovered since then that this function does not work as expected when you are specifying a view that has a multi-value field set as a category and that you checked "Show multiple values as separate entries." In this particular case, the resulting collection is sorted using the first value in the multi-value field used as a category.

-- Benoit Debuc

***************************************

That's a good tip; however, if you are doing multiple document collections there is another way.

Create a folder in the design of your database, and sort the first column the way you want your collection to be sorted.

Next, build your document collection, and use the PutAllInFolder method. Build another collection from another view, and use the PutAllInFolder method again.

When you have finished building your document collections, you can retrieve all of your documents from the folder in perfect, sorted order.

Let me know if you would like the full script for this (LS or Java).

-- Ben R.

Benoit's response:

I knew that trick, but in my case, it wasn't possible because I would have had to create close to 30 folders. My particular problem was that there is a "Print" feature in one of our applications that sends data to MS Word. By default, when using the "unprocessedDocuments," the order in Word was different than the order in the view and users complained. By sorting the collection like the view before sending the data to Word, I fixed that problem. The thing is that this "Print" feature is available in all the views, so I needed something generic that would work in any view from which the "Print" action was launched. My code will also work if I add new views in that application. If I was using the folder trick, I'd need to create a new folder every time a new view is added.

Hope this helps understand better why this code was created.

***************************************

This is a good tip. I'm using it one of my apps, but I added the two lines below. If it can't find an object (doc) it will raise an object error.

Function SortDocumentCollection 
(coll As NotesDocumentCollection, 
ViewName As
String) As NotesDocumentCollection 'This function takes a NotesdocumentCollection and sorts in the same order as the specified view. 'It actually creates a new collection by navigating through the view and adding documents 'as it encounters them in the NotesViewEntryCollection Dim session As New NotesSession Dim db As NotesDatabase Dim view As NotesView Dim entry As NotesViewEntry Dim vc As NotesViewEntryCollection Dim SortedColl As NotesDocumentCollection Dim doc As NotesDocument Set db = session.CurrentDatabase Set view = db.GetView(ViewName) Set vc = view.AllEntries 'initialise the new collection - trick is to create an empty collection because the collection needs to be initialized before we can add documents into it Set SortedColl = view. GetAllDocumentsByKey("zzxxzz") 'scan AllEntries Set entry = vc.GetFirstEntry While Not entry Is Nothing 'get a document from AllEntries and add it to the new collection Set doc = coll.GetDocument(entry.Document) if not(doc is nothing) then ' // =========================== ========== added this line (1) Call SortedColl.AddDocument(doc) end if ' // ===================== ======================= ===added this line (2) Set entry = vc.GetNextEntry(entry) Wend 'return the new collection Set SortDocumentCollection = SortedColl End Function
-- Nick W.

Do you have comments on this tip? Let us know.

Dig Deeper on LotusScript

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com'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 ...

SearchDataCenter

SearchContentManagement

Close