JavaScript - Array Sort By Value
Submitted by razormist on Thursday, January 30, 2020 - 08:32.
In this tutorial we will create a Array Sort By Value using JavaScript. This code will automatically sort the array when user select some item in the select tag. The code use onchange() function to call a specific method that sort an array and it will run a conditional statement that organize the index position of an array by providing the index position as a parameter. This 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.
There you have it we successfully created a Array Sort By Value 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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-6">
- <div class="form-inline">
- <select onchange="sortArrayData(this.value);" class="form-control">
- </select>
- </div>
- <br />
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- </tr>
- </thead>
- </table>
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will sort the array when the select tag item is selected. 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- var members = [
- {"firstname":"John", "lastname":"Smith", "gender":"Male"},
- {"firstname":"Claire", "lastname":"Temple", "gender":"Female"},
- {"firstname":"Steve", "lastname":"Roger", "gender":"Male"},
- {"firstname":"Paul", "lastname":"Miles", "gender":"Male"},
- ];
- displayData();
- function displayData(){
- var data = "";
- if(members.length > 0){
- for(var i=0; i < members.length; i++){
- data += "<tr>";
- data += "<td>"+members[i].firstname +"</td>";
- data += "<td>"+members[i].lastname+"</td>";
- data += "<td>"+members[i].gender+"</td>";
- data += "</tr>";
- }
- }
- document.getElementById('result').innerHTML = data;
- }
- function sortArrayData(str){
- if(str=="ascending"){
- sortAscending();
- }else if(str="descinding"){
- sortDescending();
- }
- }
- function sortAscending(){
- if(members.length > 0){
- var sortArray = members;
- sortArray.sort(function(a,b){
- if(a.lastname < b.lastname){
- return -1;
- }
- if(a.lastname > b.lastname){
- return 1;
- }
- return 0;
- });
- displayData();
- }
- }
- function sortDescending(){
- if(members.length > 0){
- var sortArray = members;
- sortArray.sort(function(a,b){
- if(a.lastname > b.lastname){
- return -1;
- }
- else if(a.lastname < b.lastname){
- return 1;
- }else{
- return 0;
- }
- });
- displayData();
- }
- }
Add new comment
- 108 views