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.
ambuliAsked:
Who is Participating?
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 :)
http://jsfiddle.net/AlexCode/azhpgLau/
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;
}


console.log(countsort(array));

Open in new window

0
zzynxSoftware 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;
   }
   counts[value]++;
});
array.sort(function(a, b) {
   return counts[a] < counts[b];
});
console.log(array);

Open in new window

Result:
3,3,3,3,3,4,4,4,2,2
1

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 :)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

zzynxSoftware engineerCommented:
I told myself several times that I shouldn't write code after 3AM :)
:-)
0
ambuliAuthor Commented:
Thank you guys!
0
zzynxSoftware engineerCommented:
You're welcome.
Thanx 4 axxepting
0
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
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.