Step one: build the histogram, as a map element -> its frequency for speed (assumes all elements are strings):
var histogramMap = {};
for(var i=0, len=myArray.length; i<len; i++){
var key = myArray[i];
histogramMap[key] = (histogramMap[key] || 0) + 1;
}
Step two: convert to an array of output objects:
var histogram = [];
for(key in histogramMap) histogram.push({key: key, freq: histogramMap[key]});
Step three: sort the histogram
histogram.sort(function(a,b){return b.freq - a.freq})
This also assumes that Object.prototype is not modified. This is a safe assumption to make, and lots (I think) of libraries, including jQuery, make that assumption. But, if you decide to add enumerable properties to Object.prototype, these will be picked up by for..in. If you want to be safe, modify the second step to:
var histogram = [];
for(key in histogramMap){
if(histogramMap.hasOwnProperty(i)){
histogram.push({key: key, freq: histogramMap[key]});
}
}