1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【メモ】後で削除 ソート処理

Posted at
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>重複カウント割合表示</title>
</head>
<body>
  <h2>テキスト入力</h2>
  <textarea id="inputText" rows="10" cols="30"></textarea><br>
  <button onclick="processText()">実行</button>

  <h2>結果</h2>
  <div id="result"></div>

  <script>
    function processText() {
      const text = document.getElementById('inputText').value;
      const lines = text.split('\n').map(line => line.trim()).filter(line => line !== '');

      const countMap = new Map();

      lines.forEach(line => {
        countMap.set(line, (countMap.get(line) || 0) + 1);
      });

      const total = lines.length;

      // 件数順にソート
      const sorted = Array.from(countMap.entries()).sort((a, b) => b[1] - a[1]);

      // 結果表示
      const resultDiv = document.getElementById('result');
      resultDiv.innerHTML = '';
      sorted.forEach(([text, count]) => {
        const percent = Math.round((count / total) * 100);
        resultDiv.innerHTML += `<div>${text} ${count}件(${percent}%)</div>`;
      });
    }
  </script>
</body>
</html>

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?