JavaScript - Sort Array In Table

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

In this tutorial we will create a Sort Array In Table using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

First you have to download bootstrap framework, 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 - Sort Array In Table</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="form-inline">
  17. <label>Firstname</label>
  18. <input type="text" id="firstname" class="form-control"/>
  19. <label>Lastname</label>
  20. <input type="text" id="lastname" class="form-control"/>
  21. <button type="button" onclick="addMember()" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span></button>
  22. <br /><br />
  23. </div>
  24. <br /><br />
  25. <table class="table table-bordered">
  26. <thead class="alert-info">
  27. <tr>
  28. <td>Firstname <button type="button" class="btn btn-sm btn-primary" onclick="ascFirstname()"><span class="glyphicon glyphicon-arrow-up"></span></button> <button type="button" class="btn btn-sm btn-primary" onclick="descFirstname()"><span class="glyphicon glyphicon-arrow-down"></span></button></td>
  29. <td>Lastname <button type="button" class="btn btn-sm btn-primary" onclick="ascLastname()"><span class="glyphicon glyphicon-arrow-up"></span></button> <button type="button" class="btn btn-sm btn-primary" onclick="descLastname()"><span class="glyphicon glyphicon-arrow-down"></span></button></td>
  30. </tr>
  31. </thead>
  32. <tbody id="result" style="background-color:#fff;"></tbody>
  33. </table>
  34. </div>
  35. </body>
  36. <script src="js/script.js"></script>
  37. </html>

Creating the Script

This code contains the script of the application. This code will sort the list of data base on the table category. 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 folder.

  1. var members = [];
  2.  
  3. function displayData(){
  4. var data = "";
  5. if(members.length > 0){
  6. for(var i=0; i < members.length; i++){
  7. data += "<tr>";
  8. data += "<td>"+members[i].firstname +"</td>";
  9. data += "<td>"+members[i].lastname+"</td>";
  10. data += "</tr>";
  11. }
  12. }
  13.  
  14. document.getElementById('result').innerHTML = data;
  15. }
  16.  
  17.  
  18. function addMember(){
  19. var firstname = document.getElementById('firstname');
  20. var lastname = document.getElementById('lastname');
  21.  
  22.  
  23. if(firstname.value == "" || lastname.value == ""){
  24. alert("Please enter something first!");
  25. }else{
  26. var str = '{"firstname": "'+firstname.value+'", "lastname": "'+lastname.value+'"}';
  27. var json = JSON.parse(str);
  28. members.push(json);
  29. displayData();
  30.  
  31. firstname.value = "";
  32. lastname.value = "";
  33. }
  34. }
  35.  
  36. function ascFirstname(){
  37. if(members.length > 0){
  38. var sortArray = members;
  39.  
  40. sortArray.sort(function(a,b){
  41. if(a.firstname < b.firstname){
  42. return -1;
  43. }
  44.  
  45. if(a.firstname > b.firstname){
  46. return 1;
  47. }
  48.  
  49. return 0;
  50. });
  51.  
  52.  
  53. var data = "";
  54.  
  55. if(members.length > 0){
  56. for(var i=0; i < members.length; i++){
  57. data += "<tr>";
  58. data += "<td>"+members[i].firstname +"</td>";
  59. data += "<td>"+members[i].lastname+"</td>";
  60. data += "</tr>";
  61. }
  62. }
  63.  
  64. document.getElementById('result').innerHTML = data;
  65.  
  66. }
  67.  
  68. }
  69.  
  70. function descFirstname(){
  71. if(members.length > 0){
  72. var sortArray = members;
  73.  
  74. sortArray.sort(function(a,b){
  75. if(a.firstname > b.firstname){
  76. return -1;
  77. }
  78.  
  79. else if(a.firstname < b.firstname){
  80. return 1;
  81. }else{
  82. return 0;
  83. }
  84.  
  85.  
  86. });
  87.  
  88.  
  89. var data = "";
  90.  
  91. if(members.length > 0){
  92. for(var i=0; i < members.length; i++){
  93. data += "<tr>";
  94. data += "<td>"+members[i].firstname +"</td>";
  95. data += "<td>"+members[i].lastname+"</td>";
  96. data += "</tr>";
  97. }
  98. }
  99.  
  100. document.getElementById('result').innerHTML = data;
  101.  
  102. }
  103. }
  104.  
  105. function ascLastname(){
  106. if(members.length > 0){
  107. var sortArray = members;
  108.  
  109. sortArray.sort(function(a,b){
  110. if(a.lastname < b.lastname){
  111. return -1;
  112. }
  113.  
  114. if(a.lastname > b.lastname){
  115. return 1;
  116. }
  117.  
  118. return 0;
  119. });
  120.  
  121.  
  122. var data = "";
  123.  
  124. if(members.length > 0){
  125. for(var i=0; i < members.length; i++){
  126. data += "<tr>";
  127. data += "<td>"+members[i].firstname +"</td>";
  128. data += "<td>"+members[i].lastname+"</td>";
  129. data += "</tr>";
  130. }
  131. }
  132.  
  133. document.getElementById('result').innerHTML = data;
  134.  
  135. }
  136.  
  137. }
  138.  
  139. function descLastname(){
  140. if(members.length > 0){
  141. var sortArray = members;
  142.  
  143. sortArray.sort(function(a,b){
  144. if(a.lastname > b.lastname){
  145. return -1;
  146. }
  147.  
  148. else if(a.lastname < b.lastname){
  149. return 1;
  150. }else{
  151. return 0;
  152. }
  153.  
  154.  
  155. });
  156.  
  157.  
  158. var data = "";
  159.  
  160. if(members.length > 0){
  161. for(var i=0; i < members.length; i++){
  162. data += "<tr>";
  163. data += "<td>"+members[i].firstname +"</td>";
  164. data += "<td>"+members[i].lastname+"</td>";
  165. data += "</tr>";
  166. }
  167. }
  168.  
  169. document.getElementById('result').innerHTML = data;
  170.  
  171. }
  172. }

There you have it we successfully created a 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.