JavaScript - Array Sort By Value

In this tutorial we will create a Array Sort By Value using JavaScript. This code will automatically sort the array when user select some item in the select tag. The code use onchange() function to call a specific method that sort an array and it will run a conditional statement that organize the index position of an array by providing the index position as a parameter. This is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Array Sort By Value</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-6">
  17.                         <div class="form-inline">
  18.                                 <label>Sort By</label>
  19.                                 <select onchange="sortArrayData(this.value);" class="form-control">
  20.                                         <option value="ascending">Ascending</option>
  21.                                         <option value="descending">Descending</option>
  22.                                 </select>
  23.                         </div>
  24.                         <br />
  25.                         <table class="table table-bordered">
  26.                                 <thead class="alert-info">
  27.                                         <tr>
  28.                                                 <td>Firstname</td>
  29.                                                 <td>Lastname</td>
  30.                                                 <td>Gender</td>
  31.                                         </tr>
  32.                                 </thead>
  33.                                 <tbody id="result" style="background-color:#fff;"></tbody>
  34.                         </table>
  35.                 </div>
  36.         </div>
  37. </body>
  38. <script src="js/script.js"></script>
  39. </html>

Creating the Script

This code contains the script of the application. This code will sort the array when the select tag item is selected. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. var members = [
  2.         {"firstname":"John", "lastname":"Smith", "gender":"Male"},
  3.         {"firstname":"Claire", "lastname":"Temple", "gender":"Female"},
  4.         {"firstname":"Steve", "lastname":"Roger", "gender":"Male"},
  5.         {"firstname":"Paul", "lastname":"Miles", "gender":"Male"},
  6. ];
  7.        
  8. displayData(); 
  9.        
  10. function displayData(){
  11.         var data = "";
  12.         if(members.length > 0){
  13.                 for(var i=0; i < members.length; i++){
  14.                         data += "<tr>";
  15.                         data += "<td>"+members[i].firstname +"</td>";
  16.                         data += "<td>"+members[i].lastname+"</td>";
  17.                         data += "<td>"+members[i].gender+"</td>";
  18.                         data += "</tr>";
  19.                 }
  20.         }
  21.        
  22.         document.getElementById('result').innerHTML = data;
  23. }
  24.  
  25.  
  26. function sortArrayData(str){
  27.         if(str=="ascending"){
  28.                 sortAscending();
  29.         }else if(str="descinding"){
  30.                 sortDescending();
  31.         }
  32. }
  33.  
  34. function sortAscending(){
  35.         if(members.length > 0){
  36.                 var sortArray = members;
  37.                
  38.                 sortArray.sort(function(a,b){
  39.                         if(a.lastname < b.lastname){
  40.                                 return -1;
  41.                         }
  42.                        
  43.                         if(a.lastname > b.lastname){
  44.                                 return 1;
  45.                         }
  46.                
  47.                         return 0;
  48.                 });
  49.                
  50.                
  51.                 displayData();
  52.                
  53.         }
  54.        
  55. }
  56.  
  57. function sortDescending(){
  58.         if(members.length > 0){
  59.                 var sortArray = members;
  60.                
  61.                 sortArray.sort(function(a,b){
  62.                         if(a.lastname > b.lastname){
  63.                                 return -1;
  64.                         }
  65.                        
  66.                         else if(a.lastname < b.lastname){
  67.                                 return 1;
  68.                         }else{
  69.                                 return 0;
  70.                         }
  71.                
  72.                        
  73.                 });
  74.                
  75.                 displayData();
  76.                
  77.         }
  78. }
There you have it we successfully created a Array Sort By Value using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment