How to Sort string in alphabetical order using Javascript

Create a program to sort strings in alphabetical order using the JS function. In this example, let’s create a simple web page to design and perform sort functions in Javascript.

Sort string in alphabetical order in JavaScript

Problem statement

In this, we have to create a program to sort strings in alphabetical order using the javascript function. For that create an HTML file and use the JS function that displays the result.

Solution

  • Create an HTML file
  • Create heading in <h2> </h2> tag as Convert string in alphabetical order
  • Write original string using the var var string = [“Red”,”Black”,”Orange”,”Blue”,”Purple”,”Green”,”Yellow”,”Pink”];
  • Print the original string array using document. write()  and print given string                         document.write(“<b>Original String:-</br></b>”);
    document.write(string);
  • Use sort method to sort string, string.sort();
  • Print sorting string using document.write(“<b></br>After sorting:-</br></b>”);
  • Printed sort string array using document.write(string);

JS code to sort strings in alphabetical order

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Convert string in alphabetical order</title>
</head>
<body style="text-align: center;">
    <h2 style="color: green;">Convert string in alphabetical order</h2>
<script>
var string = ["Red","Black","Orange","Blue","Purple","Green","Yellow","Pink"];
     
document.write("<b>Original String:-</br></b>");
document.write(string);
      
document.write("</br>");
      
string.sort();
      
document.write("<b></br>After sorting:-</br></b>");
document.write(string);
      
</script>                    

</body>
</html>

Output

Sort string in alphabetical order using loop in JavaScript

Problem statement 

In this task create a program to sort strings in alphabetical order using the loop in javascript. For that create an HTML file and use the JS function that displays the result.

Solution 

  • Create HTML file
  • Enter heading in bold tag <b<</b> as “Sort string using alphabetical order
  • In javascript use loop function to perform the sort  operation   while (i < str.length) { j = i + 1; while (j < str.length) { if (str[j] < str[i]) { var temp = str[i]; str[i] = str[j]; str[j] = temp; } j++; } i++; } 
  • Enter string var string = [“Kritika”, “Shahid”, “Atharva”, “Suhana”, “Raaj”,”Deepika”]; 
  • Print original string document.write(“Original String</br>”+ string +”</br>”); 
  • Call string sort method str_sort(string);
  • Print output string document.write(“</br>After sorting string is -</br>”+ string ); 

Js code to sort string using loop

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title> Sort string using loop	</title>
</head>
<body style="text-align: center;">
    <h2><b>Sort string using alphabetical order </b></h2>
<script>
function str_sort(str) {
    var i = 0, j;
    while (i < str.length) {
        j = i + 1;
        while (j < str.length) {
            if (str[j] < str[i]) {
                var temp = str[i];
                str[i] = str[j];
                str[j] = temp;
            }
            j++;
        }
        i++;
    }
}
      
var string = ["Kritika", "Shahid", "Atharva", "Suhana", "Raaj","Deepika"];
      
document.write("Original String</br>"+ string +"</br>");
      
str_sort(string);
      
document.write("</br>After sorting string is -</br>"+ string );
      
</script>                                  

</body>
</html>

Output

Sort Array of an object by property value in JavaScript

Problem statement

In this task, we have to create a program to sort an array of an object by property value in javascript. For that create an HTML file and use the JS function to display output.

Solution

  • Create HTML file
  • Enter array of string  var myArr = [ { bookName: ‘Wings of fire’, dateOfpublish: ‘1999’}, { bookName: ‘IKIGAI’, dateOfpublish: ‘Apl 2016’}, { bookName: ‘The secret’, dateOfpublish: ‘Nov 2006’ } ]; 
  • Enter the title as console.log(“Sorting array of objects by property value”); 
  • Compare the two objects function compr( obj1, obj2 ) use if loop If object 1 is lessthan object 2 then return -1  if ( obj1.bookName < obj2.bookName ){ return -1;  and object 1 is greaterthan object 2 return 1  if ( obj1.bookName > obj2.bookName ){ return 1;  otherwise return 0;
  • Print sorted array console.log(myArr.sort(compr));

JS code to sort an array of an object by property value

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        Sort array of objects by property value 
    </title>
</head> 
          
<body style = "text-align:center;"> 
    <script>
        var myArr = [
  { bookName: 'Wings of fire', dateOfpublish: '1999'},
  { bookName: 'IKIGAI', dateOfpublish: 'Apl 2016'},
  { bookName: 'The secret', dateOfpublish: 'Nov 2006' }
];
console.log("Sorting array of objects by property value");
function compr( obj1, obj2 ) {
    if ( obj1.bookName < obj2.bookName ){
      return -1;
    }
    if ( obj1.bookName > obj2.bookName ){
      return 1;
    }
    return 0;
  }

  console.log(myArr.sort(compr));

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

Output

Sort String in descending order in JavaScript

Problem statement

In this task, create a program to sort strings in descending order in javascript. For that create an HTML file and use the js function that displays the output.

Solution 

  • Create HTML file
  • Enter heading in <h2></h2> tag as Sort string in descending order
  • In function descendingOrder(str)  sort string in reverse order to use  str.sort().reverse(); 
  • Then join that strings using str1 = str.join(“”);
  • Initialize the string  var str = “m d g h c h w z s q”;  and display it using  document.write(“Original string is-“+str+”</br>”) ;
  • Use split() function to splits string into ana array of substring  str = str.split(“”); 
  • At last display string in descending order use  descendingOrder ( str ); 

JS code tp sort string in descending order

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>	Sort string in descending order</title>
</head>
<body>
    <h2 style="	color: 	blueviolet	; font-family: fantasy	; ">Sort String in descending order</h2>
<script> 
      function descendingOrder(str) {
        str.sort().reverse();
        str1 = str.join("");
        document.write(str1);
      }
 
      var str = "m d g h c h w z s q";
      document.write("Original string is-"+str+"</br>")
      str = str.split("");
     document.write("</br> "+"descendingOrder string is-		")
      descendingOrder ( str );
       

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

Output

In this way, we learn how to sort strings in alphabetical order using the JS function.