Sorting Document Collection By Date Field
I needed to sort a document collection by a date value. I found this XSnippet: http://openntf.org/XSnippets.nsf/snippet.xsp?id=sort-notesdocumentcollection-by-itemname
And it helped, but assumed the field value was a string. Here's my updated function:
function getCollection(viewName, key, fieldName) {
var db:NotesDatabase = session.getCurrentDatabase();
var vw:NotesView = db.getView(viewName);
var coll:NotesDocumentCollection = vw.getAllDocumentsByKey(key);
if (coll.getCount() == 0) return null;
// Sort the collection by date
var v:java.util.Vector = new java.util.Vector();
var doc:NotesDocument = coll.getFirstDocument();
var tm:java.util.TreeMap = new java.util.TreeMap();
while (doc != null) {
var temp:java.util.Vector = doc.getItemValue(fieldName);
tm.put(temp.elementAt(0).toJavaDate(), doc);
doc = coll.getNextDocument(doc);
}
var tmColl:java.util.Collection = tm.values();
var tmIt:java.util.Iterator = tmColl.iterator();
while (tmIt.hasNext()) {
v.add(tmIt.next())
}
return v;
}
Note that the return value is a Vector (java.util.Vector) and not a Notes Document Collection. Luckily, in a repeat (where I was using this), the repeat will just go through all the documents whether the return is a NotesDocumentCollection type object or a java.util.Vector type object.