根据 SSJS 中的日期字段对 NotesDocumentCollection 进行排序
使用服务器端 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){
}
}
Using Server side javascript, I need to sort a NotesDcumentCollection based on a field in the collection containing a date when the documents was created or any built in field when the documents was created.
It would be nice if the function could take a sort option parameter so I could put in if I want the result back in ascending or descending order.
the reason I need this is because I use database.getModifiedDocuments() which returns an unsorted notesdocumentcollection. I need to return the documents in descending order.
The following code is a modified snippet from openNTF which returns the collection in ascending order.
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){
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
构造 TreeMap 时,将 Comparator 传递给构造函数。这允许您定义自定义排序而不是“自然”排序,默认情况下按升序排序。或者,您可以针对 TreeMap 调用 DescendingMap 以按相反顺序返回克隆。
When you construct the TreeMap, pass a Comparator to the constructor. This allows you to define custom sorting instead of "natural" sorting, which by default sorts ascending. Alternatively, you can call descendingMap against the TreeMap to return a clone in reverse order.
如果您要处理大量文档,这是一种非常昂贵的方法。我主要使用 NotesViewEntrycollection(始终根据源视图排序)或视图导航器。
对于大型数据库,您可以使用一个视图,根据修改日期排序,并浏览该视图的条目,直到您的代码执行的最近日期(您必须将其保存在某个地方)。
对于较小的操作,蒂姆的方法很棒!
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!