2017-05-10 28 views
0

我正在創建一個字符串的單詞列表。然後我把這個字符串分成單個單詞,收集每個單詞重複多少次並顯示它。一切都有那裏完美的作品。但是,結果顯示的單詞和計數沒有特定的順序。我會首先以最高的數字顯示它們。我生成了以下代碼:當數字是每個條目的第一部分時,如何在Javascript中對數組進行數字排序?

<!DOCTYPE html> 
<html> 
<body> 

<p>Click the button to display the array values after the split.</p> 

<button onclick="analyze()">Analyze</button> 

<p id="displayText"></p> 

<script> 
function analyze() { 
    var str = "This this is is is is is is is is is is is is is is is just just a test test test"; 
    var res = str.split(" "); 
    document.getElementById("displayText").innerHTML = res; 
    document.getElementById("displayText").innerHTML += "<br/><br/>The amount of words is: " + res.length + "<br/><br/><br/>"; 

    document.getElementById("displayText").innerHTML += "The list of words:<br/><br/>"; 

    var words = []; 

    var wordsWithCount = []; 

    for (i = 0; i < res.length; i++) { 
     words.push(res[i]); 
     document.getElementById("displayText").innerHTML += words[i] + "<br/><br/>"; 
    } 

    var current = null; 
    var cnt = 0; 
    for (var i = 0; i < words.length; i++) { 
     if (words[i] != current) { 
      if (cnt > 0) { 
       document.getElementById("displayText").innerHTML += "<br/><br/>" + cnt + " - " + current + "<br/>"; 
       wordsWithCount.push(cnt + " - " + current); 
      } 
      current = words[i]; 
      cnt = 1; 
     } else { 
      cnt++; 
     } 
    } 

    if (cnt > 0) { 
     document.getElementById("displayText").innerHTML += "<br/><br/>" + cnt + " - " + current + "<br/>"; 
     wordsWithCount.push(cnt + " - " + current); 
    } 

    wordsWithCount.sort(); 

    document.getElementById("displayText").innerHTML += "<br/><br/><br/><br/><br/>The list of SORTED words:<br/><br/>"; 

    for (i = 0; i < wordsWithCount.length; i++) { 
     document.getElementById("displayText").innerHTML += wordsWithCount[i] + "<br/><br/>"; 
    } 
} 
</script> 

</body> 
</html> 

這是輸出的最後一位。正如你所看到的,它正在排序,但只有第一位。因此,15之前顯示2.任何想法?

的已排序的單字的列表:

1 - 這

1 - 一個

1 - 這

15 - 是

2 - 只是

3 - 測試

我很可能需要在某些時候將它分成兩個數組,因爲我希望用戶能夠複製和粘貼所有的單詞,而不需要數字。但是,我認爲這將是最後一步,因爲如果我將每個單詞的頻率分解爲它自己的數字數組,並將這些單詞保留在它們自己的數組中,那麼sort函數將對一個數組進行排序,而另一個數組數組不會遵循。

+2

'parseInt函數()'不關心是否有非數字文本* *後一個數字,字符串的開始。 – Pointy

+0

@Pointy,你能否爲我澄清一下你的答案?你是說我可以在我的代碼中使用'parseInt()'來讓它做我想要的嗎?你能提供一個我如何使用它的例子嗎? – Ryan

+0

https://stackoverflow.com/questions/15478954/sort-array-elements-string-with-numbers-natural-sort – Bergi

回答

1

使用Intl.Collator。就像這樣:

var collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}); 
var test = ['1 - this', '3 - this', '14 - this']; 
test.sort(collator.compare); 

輸出["1 - this", "3 - this", "14 - this"]

var collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}); 
 
    var test = ['1 - this', '3 - this', '14 - this']; 
 
    console.log(test.sort(collator.compare));

0

你可以只添加自定義比較函數傳遞到您的通話wordsWithCount.sort()。在這裏,我宣佈了一個名爲compareWordCount的函數,並使用了建議方法@Pointy;使用parseInt忽略所有附加到數組值的非整數部分。看看這方面的工作片段:

<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<p>Click the button to display the array values after the split.</p> 
 

 
<button onclick="analyze()">Analyze</button> 
 

 
<p id="displayText"></p> 
 

 
<script> 
 
function compareWordCount(a,b) { 
 
    if (parseInt(a) < parseInt(b)) 
 
    return -1; 
 
    return 1; 
 
} 
 

 
function analyze() { 
 
    var str = "This this is is is is is is is is is is is is is is is just just a test test test"; 
 
    var res = str.split(" "); 
 
    document.getElementById("displayText").innerHTML = res; 
 
    document.getElementById("displayText").innerHTML += "<br/><br/>The amount of words is: " + res.length + "<br/><br/><br/>"; 
 

 
    document.getElementById("displayText").innerHTML += "The list of words:<br/><br/>"; 
 

 
    var words = []; 
 

 
    var wordsWithCount = []; 
 

 
    for (i = 0; i < res.length; i++) { 
 
     words.push(res[i]); 
 
     document.getElementById("displayText").innerHTML += words[i] + "<br/><br/>"; 
 
    } 
 

 
    var current = null; 
 
    var cnt = 0; 
 
    for (var i = 0; i < words.length; i++) { 
 
     if (words[i] != current) { 
 
      if (cnt > 0) { 
 
       document.getElementById("displayText").innerHTML += "<br/><br/>" + cnt + " - " + current + "<br/>"; 
 
       wordsWithCount.push(cnt + " - " + current); 
 
      } 
 
      current = words[i]; 
 
      cnt = 1; 
 
     } else { 
 
      cnt++; 
 
     } 
 
    } 
 

 
    if (cnt > 0) { 
 
     document.getElementById("displayText").innerHTML += "<br/><br/>" + cnt + " - " + current + "<br/>"; 
 
     wordsWithCount.push(cnt + " - " + current); 
 
    } 
 

 
    wordsWithCount.sort(compareWordCount); 
 

 
    document.getElementById("displayText").innerHTML += "<br/><br/><br/><br/><br/>The list of SORTED words:<br/><br/>"; 
 

 
    for (i = 0; i < wordsWithCount.length; i++) { 
 
     document.getElementById("displayText").innerHTML += wordsWithCount[i] + "<br/><br/>"; 
 
    } 
 
} 
 
</script> 
 

 
</body> 
 
</html>

+0

您的比較器功能在兩個數字相同的情況下不正確。在這種情況下,該函數應該返回'0'而不是'1',並且是肯定的。 – Pointy

相關問題