JavaScript - Simple Array Sort

Submitted by: 
Language: 
Visitors have accessed this post 147 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Simple Array Sort</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form>
  18. <div class="form-inline">
  19. <label>Sort key</label>
  20. <select id="key" class="form-control">
  21. <option value="firstname">Firstname</option>
  22. <option value="lastname">Lastname</option>
  23. <option value="address">Address</option>
  24. </select>
  25. <button type="button" class="btn btn-primary" onclick="sort();">Sort</button>
  26. </div>
  27. </form>
  28. <br />
  29. <table class="table table-bordered">
  30. <thead class="alert-info">
  31. <tr>
  32. <td>Firstname</td>
  33. <td>Lastname</td>
  34. <td>Address</td>
  35. </tr>
  36. </thead>
  37. <tbody id="result" style="background-color:#fff;"></tbody>
  38. </table>
  39. </div>
  40. </body>
  41. <script src="js/script.js"></script>
  42. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.