There is such a script to sort the text entered by the user:

var words = prompt('Введите произвольный текст: ').split(' '); function sortingWords(words) { for (var i = 0; i < words.length-1; i++) { for (var j = 0; j < words.length-1-i; j++) { if (words[j+1] < words[j]) { var t = words[j+1]; words[j+1] = words[j]; words[j] = t; } } } console.log(words) } 

In general, I can’t figure out how to make a button click (onclick = ""), first ask the text, then transfer the entered text to the function, well, output the sorted array

  • And what do you compare when sorting? The first letter of each word? - user200141
  • @Olegg Degtev, words is an array of strings, not a string - Grundy
  • @Grundy: var words = prompt('Введите произвольный текст: ').split(' '); This thing will make an array of words. Why I clarify, suspicions creep in that the author wants to sort them by word length. - user200141
  • @Olegg Degtev, well, so this thing in question is used, lines are compared in cycles, according to the rules of string comparison. - Grundy
  • @Grundy: I ​​thought the author had a problem with sorting itself. After editing his question - it became clearer. - user200141

2 answers 2

A small addition to the answer @Romario, sorted by word length (If necessary).

To sort by word length you can use .sort() .

 function sortingWords(words) { return words.sort(function(a , b) { return a.length > b.length ? 1 : -1; }); console.log(words) } 

It remains to decide that it is necessary to do with repeated spaces, punctuation marks and special characters, since all of them will be taken into account.

      function myFunc() { // обработчик события var words = prompt('Введите произвольный текст: ') .replace(/[.!\?,;:()]/g, "") // убираем из текста ненужные символы .split(/\s+/) // получаем массив слов .sort(function (a, b) { // сортируем, без учета регистра (можно просто sort(), но это будет case sensitive) return a.toLowerCase().localeCompare(b.toLowerCase()); }); document.body.innerText = JSON.stringify(words); // можно просто console.log() или вернуть } document.getElementById('btn').addEventListener('click', myFunc); 
     <button id="btn">Click Me!</button> 

    • with .split(' ') , additional phantom array elements from empty lines appear if several spaces are entered. - Sergiks
    • @Sergiks, you propose to use the regular schedule (/ \ s + /)? - Grundy
    • @Sergiks agrees, plus everything in the function that the author cited, in this case, when breaking a word, there will be punctuation marks and other characters. For example, "Привет, как дела?" in sorted form it will look like ["дела?", "как", "Привет,"] . As I understand it, the author had a question about how to bind his function to an event. - Romario
    • @Sergiks in your version of JSON.stringify() is case sensitive accordingly will be ["Привет,","дела?","как"] . - Romario
    • one
      @Romario, in the above JSON.stringify() code, JSON.stringify() simply serializes an object / array into JSON - it doesn’t affect the output order in any way - Grundy