0

I have a number field with duplicate no's (1,1,1,1,2,2,2,2,3,4,5 etc) which defines the position of the data in a view. I would like to keep this sorting or sort the data when a database.search is performed, which returns a NotesDocumentCollection.

Ulrick Krause has posted a solution using Treemap, but a Treemap doesn't allow duplicates: http://openntf.org/XSnippets.nsf/snippet.xsp?id=sort-notesdocumentcollection-by-itemname

Any suggestions in server side JS would be really appreciated.

pipalia
  • 911
  • 1
  • 12
  • 46

1 Answers1

2

You could to this with a Vector instead of only adding the documents. Something like this:

var tm:java.util.TreeMap = new java.util.TreeMap();

while (doc != null) {
  var v:java.util.Vector;
  if (tm.containsKey(doc.getItemValueString(iName))) {
     v = tm.get(doc.getItemValueString(iName));
  }else{
     v = new java.util.Vector();
  }
  v.add(doc);
  tm.put(doc.getItemValueString(iName), v);       

  doc = col.getNextDocument(doc);
}

Then you have to iterate the Map and the containing Vectors to get your result sorted.

Edit:

And this is how you create the resulting Vector:

var rl:java.util.Vector = new java.util.Vector();  
var tCol:java.util.Collection = tm.values();
var tIt:java.util.Iterator  = tCol.iterator();

while (tIt.hasNext()) {
    v = tIt.next();
    for( var i=0; i<v.size(); i++ )
       rl.add( v.get(i) );
}

return rl;  
Sven Hasselbach
  • 10,455
  • 1
  • 18
  • 26
  • How would I pass this to a repeat control? Would I have to build a new NotesDocumentCollection by iterating the Map and the Vectors or is there a better way? – pipalia Oct 08 '12 at 17:28
  • 1
    You can use the Vector for the repeat control. I added an example to my answer. – Sven Hasselbach Oct 08 '12 at 18:51
  • Many thanks Sven. What sort of impact will this have on performance for a large dataset of over 10000 docs? – pipalia Oct 08 '12 at 20:38
  • 1
    I would expect that this code will run slower than the XSnippet. You should test it by yourself, for example you could use the Stopwatch: http://openntf.org/XSnippets.nsf/snippet.xsp?id=simple-stopwatch – Sven Hasselbach Oct 09 '12 at 07:01
  • 1
    In my opinion it will have more impact on the performance if the result of this operation is cached, because the value of a repeat control will be computed multiple times during a request. – Sven Hasselbach Oct 09 '12 at 07:03