JavaScript - Sort Column (Ascending or Descending)

In this tutorial we will create a Sort Column (Ascending or Descending) using JavaScript. This code will automatically sort the table row when the user click the button in the table header. The code use onclick() function to call a method that sort a table row by providing a string as an argument in order to undergo in a conditional statement to organize the index position using 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 - Sort Column (Ascending or Descending)</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-4">
  17. <div class="form-group">
  18. <label>Firstname</label>
  19. <input type="text" id="firstname" class="form-control"/>
  20. </div>
  21. <div class="form-group">
  22. <label>Lastname</label>
  23. <input type="text" id="lastname" class="form-control"/>
  24. </div>
  25. <center><button type="button" onclick="addMember()" class="btn btn-primary">Submit</span></button></center>
  26. </div>
  27. <div class="col-md-8">
  28. <table class="table table-bordered">
  29. <thead class="alert-info">
  30. <tr>
  31. <td>Firstname <button type="button" onclick="sortColumn('ascf')"><span class="glyphicon glyphicon-arrow-up"></span></button> <button type="button" onclick="sortColumn('descf')"><span class="glyphicon glyphicon-arrow-down"></span></button></td>
  32. <td>Lastname <button type="button" onclick="sortColumn('ascl')"><span class="glyphicon glyphicon-arrow-up"></span></button> <button type="button" onclick="sortColumn('descl')"><span class="glyphicon glyphicon-arrow-down"></span></button></td>
  33. </tr>
  34. </thead>
  35. <tbody id="result" style="background-color:#fff;"></tbody>
  36. </table>
  37. </div>
  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 table column 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.  
  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 sortColumn(sort){
  37. if(members.length > 0){
  38. var sortArray = members;
  39.  
  40.  
  41. switch(sort){
  42. case "ascf":
  43. sortArray.sort(function(a,b){
  44. if(a.firstname < b.firstname){
  45. return -1;
  46. }
  47.  
  48. if(a.firstname > b.firstname){
  49. return 1;
  50. }
  51.  
  52. return 0;
  53. });
  54. displayData();
  55. break;
  56.  
  57. case "descf":
  58. sortArray.sort(function(a,b){
  59. if(a.firstname > b.firstname){
  60. return -1;
  61. }
  62.  
  63. else if(a.firstname < b.firstname){
  64. return 1;
  65. }else{
  66. return 0;
  67. }
  68.  
  69.  
  70. });
  71.  
  72.  
  73. displayData();
  74. break;
  75.  
  76. case "ascl":
  77. sortArray.sort(function(a,b){
  78. if(a.lastname < b.lastname){
  79. return -1;
  80. }
  81.  
  82. if(a.lastname > b.lastname){
  83. return 1;
  84. }
  85.  
  86. return 0;
  87. });
  88.  
  89. displayData();
  90. break;
  91.  
  92. case "descl":
  93. sortArray.sort(function(a,b){
  94. if(a.lastname > b.lastname){
  95. return -1;
  96. }
  97.  
  98. else if(a.lastname < b.lastname){
  99. return 1;
  100. }else{
  101. return 0;
  102. }
  103.  
  104.  
  105. });
  106.  
  107. displayData();
  108. break;
  109. }
  110. }
  111. }
There you have it we successfully created a Sort Column (Ascending or Descending) 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