JavaScript - Sort Column (Ascending or Descending)

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

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="<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 - Sort Column (Ascending or Descending)</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <div class="form-group">
  19. <label>Firstname</label>
  20. <input type="text" id="firstname" class="form-control"/>
  21. </div>
  22. <div class="form-group">
  23. <label>Lastname</label>
  24. <input type="text" id="lastname" class="form-control"/>
  25. </div>
  26. <center><button type="button" onclick="addMember()" class="btn btn-primary">Submit</span></button></center>
  27. </div>
  28. <div class="col-md-8">
  29. <table class="table table-bordered">
  30. <thead class="alert-info">
  31. <tr>
  32. <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>
  33. <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>
  34. </tr>
  35. </thead>
  36. <tbody id="result" style="background-color:#fff;"></tbody>
  37. </table>
  38. </div>
  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 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

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.