PermaLinkLotusScript to sort doc collection by field value
posted Thursday 10th, June 2004
 



I was working on an agent that reads through some docs and their child docs (and their child docs, etc., etc.) to dynamically build html to store in a document and reference from computed text on a subform on a web doc ...to allow a javascript/html selection menu to adjust as the background documents are changed. Right. Well anyway, it became apparent that in order to allow each submenu to display its choices in alphabetic order, I needed to be able to sort document collections by the value of a given field. I found various routines on the web to sort document collections, but not based on any named field.

So here's a LotusScript routine to sort a NotesDocumentCollection using the value of a named field as the sort key. The input parameters are an unsorted document collection, and the name of the field on which to sort. The output is a sorted collection. I've included the actual sort function that my routine calls; it's something I downloaded from Notes Net or the Sandbox or somewhere some years ago.
Comments :
 
 

1. Posted by Stan Rogers09/09/2007 08:42 PM



I'd make one small change to that. Since the text of the selected field is being used as a list tag, it has to be unique. To avoid problems with documents having duplicate values, I usually append a space and the UNID. Duplicates will then sort by creation date rather than being ignored or replaced in the list.




2. Posted by Joe Litton - website09/09/2007 08:42 PM



Stan,
Thanks for another great idea. In my current app, the data is such that the sort field always will be unique. But the idea of putting this code into functions (and into a script library) is for reusability, so it needs the tweak you've mentioned. I've added a space and the UNID to the end of each listtag and updated the posted code.
THANKS!




3. Posted by Jens - website09/09/2007 08:42 PM



Just came accross this code today .....

Happy to see very clean and very professional code here. There is lot of code available in the public, but most of the code is not half as good as this one.

Here a little enhancement for the TimeDate conversion. As TimeDate values internaly are stored as double values, which have a Level Set date of 1.1.1900, there is no need for a conversion (it is basically the same as you did already internally). And there is a simpler version to make the resulting string of a fix length of 10 characters.

Here is my take of that section:

Case DATETIMES
Dim dateTime As NotesDateTime
set dateTiem = item.DateTimeValue
Dim dDiff As Double
dDiff = dateTime.LSLocalTime * 86400 ' this gives the difference in seconds from 1.1.1900
dDiff = fix (dDiff) ' should not be necessary, but in case, we strip any fractional part
strTemp = Cstr(dDiff) ' Pad with "0" as needed on left to get 10-character string
strTemp = right$("0000000000" + strTemp,10)




4. Posted by Joe Litton - website09/09/2007 08:42 PM



Jens,

Ah! Thank you for the code simplification. I have edited the routine to incorporate your suggestion (with proper credit given) and will upload this evening.

Cheers




5. Posted by Reid09/09/2007 08:42 PM



Joe,
I just found this site and topic and can't thank you enough. I was getting so fustrated at the fact that a notesdocumentcollection was not sorted. This works so well and does exactly what I need.

I can't wait to poke around to see what other neat tips and samples can be found. I feel like I struck gold!!!!



A big thank you
Reid