JavaScript - Sorting Array Table By Lastname

In this tutorial we will create a Sorting Array Table By Lastname using JavaScript. This code will automatically sort the array table when the user click a button. The code use onclick() function to initiate a method that sort an array by base on the click button then it will be undergoing in a conditional statement that organize the index position of an array by the use of sortArray(). You can apply this script to your code to make your transaction faster, it 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 - Sorting Array Table By Lastname</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-2"></div>
  17.                 <div class="col-md-8">
  18.                         <button type="button" class="btn btn-primary" onclick="sortAscending()">Ascending</button> <button type="button" class="btn btn-primary" onclick="sortDescending()">Descending</button>
  19.                        
  20.                         <br /><br />
  21.                        
  22.                         <table class="table table-bordered">
  23.                                 <thead class="alert-info">
  24.                                         <tr>
  25.                                                 <td>Firstname</td>
  26.                                                 <td>Lastname </td>
  27.                                         </tr>
  28.                                 </thead>
  29.                                 <tbody id="result" style="background-color:#fff;"></tbody>
  30.                         </table>
  31.                 </div>
  32.         </div>
  33. </body>
  34. <script src="js/script.js"></script>
  35. </html>

Creating the Script

This code contains the script of the application. This code will sort the array when the button is clicked. 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"},
  3.         {"firstname":"Claire", "lastname":"Temple"},
  4.         {"firstname":"Steve", "lastname":"Roger"},
  5.         {"firstname":"Paul", "lastname":"Miles"},
  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 += "</tr>";
  18.                 }
  19.         }
  20.        
  21.         document.getElementById('result').innerHTML = data;
  22. }
  23.  
  24.  
  25. function sortAscending(){
  26.         if(members.length > 0){
  27.                 var sortArray = members;
  28.                
  29.                 sortArray.sort(function(a,b){
  30.                         if(a.lastname < b.lastname){
  31.                                 return -1;
  32.                         }
  33.                        
  34.                         if(a.lastname > b.lastname){
  35.                                 return 1;
  36.                         }
  37.                
  38.                         return 0;
  39.                 });
  40.                
  41.                
  42.                 var data = "";
  43.                
  44.                 if(members.length > 0){
  45.                         for(var i=0; i < members.length; i++){
  46.                                 data += "<tr>";
  47.                                 data += "<td>"+members[i].firstname +"</td>";
  48.                                 data += "<td>"+members[i].lastname+"</td>";
  49.                                 data += "</tr>";
  50.                         }
  51.                 }
  52.                
  53.                 document.getElementById('result').innerHTML = data;
  54.                
  55.         }
  56.        
  57. }
  58.  
  59. function sortDescending(){
  60.         if(members.length > 0){
  61.                 var sortArray = members;
  62.                
  63.                 sortArray.sort(function(a,b){
  64.                         if(a.lastname > b.lastname){
  65.                                 return -1;
  66.                         }
  67.                        
  68.                         else if(a.lastname < b.lastname){
  69.                                 return 1;
  70.                         }else{
  71.                                 return 0;
  72.                         }
  73.                
  74.                        
  75.                 });
  76.                
  77.                
  78.                 var data = "";
  79.                
  80.                 if(members.length > 0){
  81.                         for(var i=0; i < members.length; i++){
  82.                                 data += "<tr>";
  83.                                 data += "<td>"+members[i].firstname +"</td>";
  84.                                 data += "<td>"+members[i].lastname+"</td>";
  85.                                 data += "</tr>";
  86.                         }
  87.                 }
  88.                
  89.                 document.getElementById('result').innerHTML = data;
  90.                
  91.         }
  92. }
There you have it we successfully created a Sorting Array Table By Lastname 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