1

使用服务器端 javascript,我需要根据集合中的字段对 NotesDcumentCollection 进行排序,该字段包含创建文档的日期或创建文档时的任何内置字段。

如果该函数可以采用排序选项参数,那就太好了,这样如果我希望结果按升序或降序返回,我就可以输入。

我需要这个的原因是因为我使用了 database.getModifiedDocuments() ,它返回一个未排序的 notesdocumentcollection。我需要按降序返回文件。

以下代码是来自 openNTF 的修改片段,它按升序返回集合。

function sortColByDateItem(dc:NotesDocumentCollection, iName:String) {
try{    
    var rl:java.util.Vector = new java.util.Vector();
    var tm:java.util.TreeMap = new java.util.TreeMap();
    var doc:NotesNotesDocument = dc.getFirstDocument();

    while (doc != null) {
        tm.put(doc.getItemValueDateTimeArray(iName)[0].toJavaDate(), doc);
        doc = dc.getNextDocument(doc);
    }
    var tCol:java.util.Collection = tm.values();
    var tIt:java.util.Iterator  = tCol.iterator();
    while (tIt.hasNext()) {
       rl.add(tIt.next());
    }
    return rl;  
}catch(e){

}
}
4

2 回答 2

3

构造 TreeMap 时,将 Comparator 传递给构造函数。这允许您定义自定义排序而不是“自然”排序,默认情况下按升序排序。或者,您可以针对 TreeMap 调用 descendingMap 以以相反的顺序返回克隆。

于 2012-02-10T20:04:48.197 回答
0

This is a very expensive methodology if you are dealing with large number of documents. I mostly use NotesViewEntrycollection (always sorted according to the source view) or view navigator.

For large databases, you may use a view, sorted according to the modified date and navigate through entries of that view until the most recent date your code has been executed (which you have to save it somewhere).

For smaller operations, Tim's method is great!

于 2012-02-10T22:15:08.947 回答