pipalia
pipalia

Reputation: 911

Sort documentcollection with duplicate numbers

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.

Upvotes: 0

Views: 375

Answers (1)

Sven Hasselbach
Sven Hasselbach

Reputation: 10485

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;  

Upvotes: 2

Related Questions