JavaScript - Simple Array Sort

In this tutorial we will create a Simple Array Sort using JavaScript. This code will automatically sort the array table when the user select a value from an option. The code use onclick() function to initiate a method that sort an array by getting the option value and compare in switch() in order to determine the value to be sorted, after that the array list will be undergoing in conditional statement to organize the index position 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 - Simple Array Sort</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <form>
  17. <div class="form-inline">
  18. <label>Sort key</label>
  19. <select id="key" class="form-control">
  20. <option value="firstname">Firstname</option>
  21. <option value="lastname">Lastname</option>
  22. <option value="address">Address</option>
  23. </select>
  24. <button type="button" class="btn btn-primary" onclick="sort();">Sort</button>
  25. </div>
  26. </form>
  27. <br />
  28. <table class="table table-bordered">
  29. <thead class="alert-info">
  30. <tr>
  31. <td>Firstname</td>
  32. <td>Lastname</td>
  33. <td>Address</td>
  34. </tr>
  35. </thead>
  36. <tbody id="result" style="background-color:#fff;"></tbody>
  37. </table>
  38. </div>
  39. </body>
  40. <script src="js/script.js"></script>
  41. </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", "address": "Tokyo"},
  3. {"firstname": "Claire", "lastname": "Temple", "address": "Racoon City"},
  4. {"firstname": "Natasha", "lastname": "Romanof", "address": "Nazi"}
  5. ];
  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].address+"</td>";
  18. data += "</tr>";
  19. }
  20. }
  21.  
  22. document.getElementById('result').innerHTML = data;
  23. }
  24.  
  25. function sort(){
  26. var val=document.getElementById('key').value;
  27.  
  28. switch(val){
  29. case"firstname":
  30. if(members.length > 0){
  31. var sortArray = members;
  32.  
  33. sortArray.sort(function(a,b){
  34. if(a.firstname < b.firstname){
  35. return -1;
  36. }
  37.  
  38. if(a.firstname > b.firstname){
  39. return 1;
  40. }
  41.  
  42. return 0;
  43. });
  44.  
  45.  
  46. var data = "";
  47.  
  48. if(members.length > 0){
  49. for(var i=0; i < members.length; i++){
  50. data += "<tr>";
  51. data += "<td>"+members[i].firstname +"</td>";
  52. data += "<td>"+members[i].lastname+"</td>";
  53. data += "<td>"+members[i].address+"</td>";
  54. data += "</tr>";
  55. }
  56. }
  57.  
  58. document.getElementById('result').innerHTML = data;
  59.  
  60. }
  61. break;
  62. case"lastname":
  63. if(members.length > 0){
  64. var sortArray = members;
  65.  
  66. sortArray.sort(function(a,b){
  67. if(a.lastname < b.lastname){
  68. return -1;
  69. }
  70.  
  71. if(a.lastname > b.lastname){
  72. return 1;
  73. }
  74.  
  75. return 0;
  76. });
  77.  
  78.  
  79. var data = "";
  80.  
  81. if(members.length > 0){
  82. for(var i=0; i < members.length; i++){
  83. data += "<tr>";
  84. data += "<td>"+members[i].firstname +"</td>";
  85. data += "<td>"+members[i].lastname+"</td>";
  86. data += "<td>"+members[i].address+"</td>";
  87. data += "</tr>";
  88. }
  89. }
  90.  
  91. document.getElementById('result').innerHTML = data;
  92.  
  93. }
  94. break;
  95. case"address":
  96. if(members.length > 0){
  97. var sortArray = members;
  98.  
  99. sortArray.sort(function(a,b){
  100. if(a.address < b.address){
  101. return -1;
  102. }
  103.  
  104. if(a.address > b.address){
  105. return 1;
  106. }
  107.  
  108. return 0;
  109. });
  110.  
  111.  
  112. var data = "";
  113.  
  114. if(members.length > 0){
  115. for(var i=0; i < members.length; i++){
  116. data += "<tr>";
  117. data += "<td>"+members[i].firstname +"</td>";
  118. data += "<td>"+members[i].lastname+"</td>";
  119. data += "<td>"+members[i].address+"</td>";
  120. data += "</tr>";
  121. }
  122. }
  123.  
  124. document.getElementById('result').innerHTML = data;
  125.  
  126. }
  127. break;
  128. }
  129. }
There you have it we successfully created a Simple Array Sort 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