This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Sep 5, 2011, 2:17 PM
178 Posts
topic has been resolvedResolved

Sorting a vector in SSJS

  • Category: Other
  • Platform: All
  • Release: 8.5.3
  • Role: Developer
  • Tags: vector,java,collections,sorting,sorting repeat control
  • Replies: 8
For a repeat control I have a vector as source.
 
I would like to sort the vector before I send it to the repeat control but I experience some troubles with it.
 
Information on sorting a vector in java:
http://www.java-examples.com/sort-java-vector-descending-order-using-comparator-example 
 
When I call in my code:
Collections.sort(documents);
 
I get the message:
[ReferenceError] 'Collections' not found
 
So I wonder how I should import the java collections utily in SSJS?
 
Sep 5, 2011, 3:13 PM
23 Posts
Re: Sorting a vector in SSJS
 Based on the example code use either...
 
java.util.Collections.sort(documents);
 
or
 
importPackage(java.util); 
Collections.sort(documents);
 
You might also consider using a TreeMap which auto sorts.  I have some example code here for download: http://www.rgmconsulting.com/xpageu .
 
 
Sep 5, 2011, 6:18 PM
178 Posts
Re: Sorting a vector in SSJS
 both methods trow:
 
Error calling method 'sort(java.util.Vector)' on java class 'java.util.Collections' 
Sep 5, 2011, 8:09 PM
23 Posts
Re: Sorting a vector in SSJS
 Hmmm...this works fine...
 

var v:java.util.Vector=new java.util.Vector();

v.addElement("1");

v.addElement("2");

v.addElement("3");


var c:java.util.Comparator=java.util.Collections.reverseOrder();


java.util.Collections.sort(v,c);

print(v)
 
...and so does this... 
 

var v:java.util.Vector=new java.util.Vector();

v.addElement("3");

v.addElement("2");

v.addElement("1");


var c:java.util.Comparator=java.util.Collections.reverseOrder();


java.util.Collections.sort(v);

print(v) 
 
 
...so the error must be something else in your code.  Maybe you really don't have a Vector. 
Sep 6, 2011, 6:41 AM
178 Posts
Re: Sorting a vector in SSJS
Here is how i build the vector:
 
function getFilteredFTContent() {
var documents:java.util.Vector = new java.util.Vector();
var server = sessionScope.get("applibServer");
var dbname = sessionScope.get("applibFile")
        var lkview = sessionScope.get("applibLKView") 
var applib:NotesDatabase = session.getDatabase(server, dbname, false);
var itemView:NotesView = applib.getView(lkview);
var country = getCountrySelected();
if (country != null){
itemView.clear();
itemView.FTSearch(buildCountryQuery(country));
var viewEntryCol:NotesViewEntryCollection = itemView.getAllEntries();
}
else{
itemView.clear();
var viewEntryCol:NotesViewEntryCollection = itemView.getAllEntries();
}
var viewEntry:NotesViewEntry = viewEntryCol.getFirstEntry();
while (viewEntry != null) {
var doc:NotesDocument = viewEntry.getDocument();
documents.addElement(doc);
viewEntry = viewEntryCol.getNextEntry(viewEntry);
}
       return documents
}
 
In case I add 
 
var c:java.util.Comparator=java.util.Collections.reverseOrder();
java.util.Collections.sort(documents,c); 
 
I get the runtime error: 
Error calling method 'sort(java.util.Vector, java.util.Collections$ReverseComparator)' on java class 'java.util.Collections'
lotus.domino.local.Document incompatible with java.lang.Comparable 
Sep 6, 2011, 1:30 PM
23 Posts
Re: Sorting a vector in SSJS
 My guess is that the comparator does not work with document objects.
 
You could do a quick test by simply changing your addElement to add the UNID of each document and seeing what happens.
 
It seems like you are sorting search results...TreeMap my friend...or wait until 8.5.3 where it is rumored you can sort search results directly. 
Sep 7, 2011, 12:15 PM
272 Posts
Re: Sorting a vector in SSJS
Hi,
 
if you just want to reverse the order, you could use getLastEntry() and the go backward using getPrevEntry() in your NotesViewEntryCollection.
 
Sven
Sep 9, 2011, 10:08 AM
178 Posts
Re: Sorting a vector in SSJS
 yeah sorting vector on documents does not work so for now I push the results in a multi-dimensional array (titel + document ID) and sort that one by title.
 
getalldocumentsbykey results at least in a sorted result but then the design of the view was not very usefull too me without asking for a design request .
 
can't wait for that additional sorting capacity on a FTsearch !! =) 
Sep 9, 2011, 2:51 PM
3 Posts
Re: Sorting a vector in SSJS

Another solution : 

  1. Iterate your view and put your elements in a java.util.TreeMap with the key you want to sort to
  2. Return the TreeMap.values() to your repeat control
  3. if you want to get the descending order return TreeMap.descendingMap().values()

 


This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal