Sort Array Index By Gender Using JavaScript
Submitted by razormist on Thursday, May 14, 2020 - 20:06.
This tutorial will teach you how to do Sort Array Index By Gender using JavaScript. The program can sort a certain array object by adding a condition in the index position. This is a free code and you can apply this to your working projects. To learn more about this, just follow the steps below.
There you have it we successfully created a Sort Array Index By Gender 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:
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.- <!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-8">
- <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. The code will sort the array object by gender when user click the button. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.- var members = [
- {"firstname":"John", "lastname":"Smith", "gender": "Male"},
- {"firstname":"Claire", "lastname":"Temple", "gender": "Female"},
- {"firstname":"Hailee", "lastname":"Stenfield", "gender": "Female"},
- ];
- 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 sortAscending(){
- if(members.length > 0){
- var sortArray = members;
- sortArray.sort(function(a,b){
- if(a.gender < b.gender){
- return -1;
- }
- if(a.gender > b.gender){
- return 1;
- }
- return 0;
- });
- 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 sortDescending(){
- if(members.length > 0){
- var sortArray = members;
- sortArray.sort(function(a,b){
- if(a.gender > b.gender){
- return -1;
- }
- else if(a.gender < b.gender){
- return 1;
- }else{
- return 0;
- }
- });
- 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;
- }
- }
Add new comment
- 699 views