javascript sort an array by number of occurences

Hi there,
I have an array:
  var array = [ 3, 4, 2, 3, 2, 3, 3, 3, 4, 4];
I want to sort it so that it will become [3, 3 ,3, 3, 4, 4, 4, 2, 2]
Thank you.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
It's pretty late here but I managed to assemble some code :)
var array = [ 3, 4, 2, 3, 2, 3, 3, 3, 4, 4];

function countsort(array){
    var getCount = function(){
    	var result = {};
        for(var i=0; i<array.length; i++){
        	result[array[i]] = (result[array[i]] ? result[array[i]] : 0) + 1;
        return result;
    var calCount = getCount();
    var sorted = [];
    for(var p in calCount){
    	sorted.push(calCount[p] + ':' + p);
    sorted = sorted.sort(function(a,b){
        var _a = a.split(':')[0];
        var _b = b.split(':')[0];
    	return _a-_b;
    var result = '';
    for(var k=sorted.length-1; k>=0; k--){
        var item = sorted[k].split(':');
    	result += Array(item[0]*1+1).join(item[1]);
    return result;


Open in new window

zzynxSr. Software engineerCommented:
var array = [ 3, 4, 2, 3, 2, 3, 3, 3, 4, 4];
var counts = {};
array.forEach(function(value) {
   if(!counts[value]) {
       counts[value] = 0;
array.sort(function(a, b) {
   return counts[a] < counts[b];

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexandre SimõesManager / Technology SpecialistCommented:
Good one @zzynx.
I told myself several times that I shouldn't write code after 3AM :)
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

zzynxSr. Software engineerCommented:
I told myself several times that I shouldn't write code after 3AM :)
ambuliAuthor Commented:
Thank you guys!
zzynxSr. Software engineerCommented:
You're welcome.
Thanx 4 axxepting
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.