Sort array by frequency
问题 I would like to count how many times is every string in array and sort them by number of times they exist in array I have this code now hlasy.sort(); var zaciatok = null; var pocet = 0; for (var i = 0; i < hlasy.length; i++) { if (hlasy[i] != zaciatok) { if (pocet > 0) { console.log(zaciatok + ' má ' + pocet + ' hlasov'); } zaciatok = hlasy[i]; pocet = 1; } else { pocet++; } } if (pocet > 0) { console.log(zaciatok + ' má ' + pocet + ' Hlasov'); } it works, but it outputs strings from array