I thought I’d post this as I have not seen anyone post anything similar and figure it might help others who need the same type of functionality.
I had an XPages application where a list of documents was only to be displayed via the results of a full text search method. Basically one xpage was used to allow the user to enter values in fields for defining the search criteria and submit it, with the search results then being displayed in another xpage with a repeater control displaying the collection of documents. Easy enough. The hitch though was that the results had to be sorted in a particular order and the results page also had to allow the end user to sort the results again based on various other fields besides the default field used for sorting. This posed a problem as even in xpages there is no built-in method provided for sorting the results of full text searches. So I had to write my own process in SSJS to perform the search, load up the necessary values to be sorted into an array along with the UNID for each document, sort them, then parse the values of the sorted array to build a new document collection in the properly sorted order and return the sorted collection to the repeater control for displaying the results.
The below code is what I ended up with. The code needs to be placed in the data binding section for the repeat control. A couple of things to note. First, there is a session scope variable call “searchQuery” which contains the criteria for the search that was defined by a user when submitted via a previous xpage. There’s another session variable called “searchSortByField” which contains the name of the field which has the values that will be used to sort the search results - additionally the xpage contains a combobox field that is tied to the “searchSortByField” session variable and it contains a list of the each field that can be used as a primary sorting key - such that whenever the value of the combobox field is changed then the search results automatically re-sort in the repeat control. The performance was quite satisfactory when I tried this with a local database of about 80,000 records, but don’t know how well it will perform for very large databases/result sets so your milage may vary.
=============================
var targetDB:NotesDatabase = session.getDatabase(session.getServerName(),“APPS\Test.nsf”);
var unsortedDC:NotesDocumentCollection = targetDB.FTSearch(sessionScope.get(“searchQuery”));
if((unsortedDC==null)||(unsortedDC.getCount()==0)){
requestScope.put("computedMessage","No matching documents found.");
return null;
}
var counter = 0;
var dcDoc:NotesDocument = unsortedDC.getFirstDocument();
var listToSort = new Array();
var sortByFieldName;
if((sessionScope.get(“searchSortByField”)==null)||(sessionScope.get(“searchSortByField”)==“”)){
sortByFieldName = "RequestNo";
}else{
sortByFieldName = sessionScope.get("searchSortByField")
}
while (dcDoc!=null){
if(sortByFieldName=="RequestNo"){
listToSort[counter]=dcDoc.getItemValueString(sortByFieldName)+ "~" + dcDoc.getUniversalID();
}else{
listToSort[counter]=dcDoc.getItemValueString(sortByFieldName)+ "&" + dcDoc.getItemValueString("RequestNo") + "~" + dcDoc.getUniversalID(); }
dcDoc = unsortedDC.getNextDocument(dcDoc);
counter++;
}
listToSort.sort();
var sortedDC:NotesDocumentCollection = searchDB.getProfileDocCollection(“GiveMeAnEmptyCollection”);
for ( var i=0;i<listToSort.length;i++ ){
stringToParse = listToSort[i];
startPos = stringToParse.indexOf("~");
UNID=stringToParse.substring(startPos+1, stringToParse.length);
var notesDoc:NotesDocument = targetDB.getDocumentByUNID(UNID);
sortedDC.addDocument(notesDoc);
}
return sortedDC;