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 20, 2011, 9:08 PM
14 Posts

Sort an array in JavaScript

  • Category: Other
  • Platform: All
  • Release: 8.5.1
  • Role: Developer
  • Tags: javascript array sort date descending
  • Replies: 2
 
 I've got an array of documents assembled by FTSearch from a source view. 
 
I'm trying to return only the first 500 records from the array.  However, everything I've tried - .slice(), .splice(), .subtract - returns an incomplete set of documents (random records are removed).
 
The approach I'm trying now is to make a copy of the original array; sort it by date (a field on the documents), then .slice 500 docs from it.
 
But I'm not sure exactly how to sort the second array by date in JavaScript.  Has anyone done this before?
 
Thanks very much,
Sally
 

Sep 20, 2011, 11:30 PM
94 Posts
Re: Sort an array in JavaScript
You can sort the array by date if you first format the date as yyyymmdd.  In this example, I had an array of docunids.  I retrieved the corresponding docs and then sorted them by customer name (a field in the docs) and returned a doc collection.  This should help you get started.
 
    //build a dcol
    //sort by cusnm first
    var newarr = new Array();
    var idarr = sessionScope.QResUNIDs;
    if (idarr==null){return(null);    }
    var doc = null;
    for(x=0;x<idarr.length;x++){
        doc = database.getDocumentByUNID(idarr[x]);
        newarr.push(doc.getItemValueString("CusNm") + "~" + doc.getUniversalID());
    }
   //sort the array
    var xentrystr = "";
    var yentrystr = "";
    var xtmp = "";
    var ytmp = "";
    var entryarr = new Array();
for(x=0;x<newarr.length;x++){
    for(y=0;y<newarr.length;y++){
        xentrystr = newarr[x];
        entryarr = xentrystr.split("~");
        xtmp = entryarr[0];
        yentrystr = newarr[y];
        entryarr = yentrystr.split("~");
        ytmp = entryarr[0];
        if(xtmp<ytmp){
            stmp = newarr[x];
            newarr[x] = newarr[y];
            newarr[y] = stmp;
        }
    }
}

//return a dcol
var stmp = newarr.join(",");
var pos = stmp.indexOf(",");
if(pos=-1){  //none or one
    if(stmp==""){
        return(null);
    }
}
var newdcol = database.createDocumentCollection();
for(n=0;n<newarr.length;n++){
    stmp = newarr[n];
    entryarr = stmp.split("~");
    stmp = entryarr[1];
    doc = database.getDocumentByUNID(stmp)
    if (doc!=null){
        newdcol.addDocument(doc);
    }       
}   

    return(newdcol);
}
 
 
Sep 21, 2011, 7:16 AM
261 Posts
Re: Sort an array in JavaScript
Sally,
 
Your observation is correct: an FTSearch resultset isn't sorted (although it can be starting at the upcoming 8.5.3 release using the new FTSearchSorted method...)
 
Anyway, the approach I'd take:
  • first process all the documents from the resultset: copy the values you need to a JavaScript object array, e.g.:
//create the array
var results = [];
 
// add every document from the resultset to the array using the .push() method in a loop function (adjust it to retrieve the values you need):
results.push( 
  id : doc.getUniversalID(),
  someField : doc.getItemValueString("someField"),
  • you can now sort the array using the JavaScript sort method combined with a sort function, e.g.:
var dateSortAscending = function(a,b) {
   var date1 = a.dateField; var date2 = b.dateField;
   if (date1 > date2) return 1;
   if (date1 < date2) return -1;
  return 0;
 
results.sort( dateSortAscending );
  • you can get the first 500 items from the sorted array  using results.slice(0,500).
Mark

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