Words Sorting of a stings in JavaScript

Create a program to sort the words of string using the JS function.  In this example, let’s create a simple web page that to design and performs sort operations using Javascript, HTML, CSS.

Sort words of string in Java Script using split()

Problem statement

In this article, we have to sort the words of string using the javascript functions. For that create html file and use the JS function that displays the result.

Solution 

  • Create html file
  • Create heading using the <p></p> tag as Sort words of a string 
  • In javascript take input from user using prompt   const string = prompt(‘Enter a sentence: ‘);
  • Converting string to an array using string.split(‘ ‘);
  • Sort array of elements using words. sort();
  • Display sorted words using document.write(‘<b>The sorted words are:</b>’+”</br>”);
  • Use for loop for (const element of words) { in that print the elements of words using document.write(element+ “</br>”);

JS code to display sorting  words of string using JS function

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Sort words using JS</title>
</head>
<body style="text-align: center;">
    
    <p style="color: purple;font-weight: bold;font-style: italic;font-size: medium;">
        Sorting Words of strings 
    </p>
<script type="text/javascript">
    const string = prompt('Enter a sentence: ');

    const words = string.split(' ');

    words.sort();

    document.write('<b>The sorted words are:</b>'+"</br>");

    for (const element of words) {
  	document.write(element+ "</br>");
    }

</script>
</body>
</html>

Output

 

Sort words of string based on the number present in each word

Problem statement 

In this task, create a program to sort string words of string based on the number present in each word using javascript. For that create Html file and use the js function that display output.

Solution 

  • Create Html file
  • Enter heading as Sort string of words
  • Enter string  var str = “gam2e my3 M1y rul4es”;  print that string using  document.write(“The original string is -“+ str+”</br>”); 
  • Declare sortByNum () to sort string by its number  and findNum () functions  for find the number in string
  • Use split() method to split the string to sort and use reduce() method executes a reducer function for string elements .
  • Declare the  str.split(‘ ‘) and initialize sorter =(a,b) function which return findNum(a) -findNum(b) result.
  • Then use array sort method to sort string and then combine string using join function   arr.sort(sorter); return arr.join(‘ ‘); 
  • Print sorted array using document.write(“</br>”+”Sorted string is -“+ sortByNum(str)); 

JS code to sort string of words based on number present

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <h2 style="color: gold; font-weight: bold;">Sort string of words </h2>
<script type="text/javascript">
    var str = "gam2e my3 M1y rul4es";
    document.write("The original string is -"+ str+"</br>");
    var sortByNum = (str = '') => {
   var findNum = (s = '') => s
      .split('')
      .reduce((acc, val) => +val ? +val : acc, 0);
   var arr = str.split(' ');
   var sorter = (a, b) => {
      return findNum(a) - findNum(b);
   };
   arr.sort(sorter);
   return arr.join(' ');
};
document.write("</br>"+"Sorted string is -"+ sortByNum(str));

</script>
</body>
</html>

Output

 

In this way, we will learn how to sort words of string using js.