Sorting object in javaScript

2

I need to sort the following object in descending order and after that I should delete everything that comes after "_" and even it, to see which word is formed:

Here's what I've done:

var str = 'string muito grande para o comentário';
var count = {};

str.split('').forEach(function(v) {

 if (v === ' ') return;
 v = v.toLowerCase();
 count[v] = count[v] ? count[v] + 1 : 1; 

}); 

console.log(count);


count{
    a:94 
    b:93
    c:88
    d:87
    e:91
    f:86
    g:80
    h:83
    i:78
    j:79
    k:82
    l:92
    m:74
    n:99
    o:96
    p:98
    q:84
    r:97
    s:77
    t:81
    u:100
    v:95
    w:75
    x:89
    y:85
    z:76
    _:90
}
    
asked by anonymous 05.12.2017 / 21:43

1 answer

2

As your final intention is to get the word, just use the function below:

var str = 'string muito grande para o _ comentário'; 
var count = {}; 


str.split('').forEach(function(v) { 
	if (v === ' ') 
		return; 
	v = v.toLowerCase(); 
	count[v] = count[v] ? count[v] + 1 : 1; 
}) 

function palavra(hashTable){
  return Object.keys(hashTable).sort(function(a, b){
    //Ordenacao decrescente
    return hashTable[b] - hashTable[a];

  }).reduce(function(p, c){ 
      //Formacao da palavra
      return p+c
  }).split('_')[0];//Texto antes de _
  
}

console.log(palavra(count))
    
06.12.2017 / 15:42